Saturday, March 20, 2021

Go Lang - Installation and Basic Tests

MacOS 11.2.3 - Big Sur
Go Language 1.16.2 - darwin/amd64




Goals:
  • Start learning Go Language from scratch.

Information:

  • Basic commands:
    • go version
    • go env
      • $GOPATH controls where the Go compiler and tools will look for source code.
    • go help <command>
    • go get <lib-name> - automatically install dependencies (e.g:  go get -u github.com/gen2brain/dlgs)
    • go run <file.go> - this command first compiles the specified file(s), then executes the resulting binary.
    • go build <file.go> - command to compile our code into an executable binary.
    • go install <file.go> - command works like `go build`, except instead of putting the binary file in the source code folder, it installs it to $GOPATH/bin.
    • godoc -http=:6060 - extracts and generates documentation for Go programs. With the -http flag, it runs as a web server and presents the documentation as a web page.
  • Basic rules:
    • A function that returns values must declare it, the type declaration follows the function name. Every function that declares a return type, must end with a return statement.
func greeting() string {
return "Hello world"
}

    • Functions imported from another package are always namespaced with the package name (e.g: lib.SomeFunction() )
    • Visible External Functions, Variables and Methods starts with Capital Letter.
    • In any  `.go file, the first non-comment line is a package declaration. The package declaration is mandatory. If the file is in a subfolder of the project its package must have the name of the subfolder. Every  `.go file in the same folder must have the same package name.
    • By convention, 3rd-party packages are named after their repository URL. For example, a xpto library hosted on Github would be imported as "github.com/<user-or-company-name>/xpto". The application is in xpto.go. This file declares its package as main, and defines a main() function. This tells the compiler to generate an executable from the file. The package can import from the standard library, and from our lib package specified by its path.
/$GOPATH
|--- /src
      |--- /github.com
            |--- /<user-or-company-name>
                  |--- /xpto
                  |   |__ xpto.go
                  |--- /lib
                      |__ util.go

    • A variable must be declared as a specific type before a value can be assigned to it. Once declared, a variable may only be assigned values of its declared type. Go also provides an operator, :=, that combined declaration and assignment in the same statement.
    • Our Hello World program declares its package as main, and contains a special function main(). That tells Go to compile this code as an executable program, with the entry point at main(). The function main() has no arguments and no return value.


Install:

  • MacOS
    • Download and install it from here.
      • Open the package file you downloaded and follow the prompts to install Go.
      • The package installs the Go distribution to /usr/local/go.
      • The package should put the /usr/local/go/bin directory in your PATH environment variable.
      • You may need to restart any open Terminal sessions for the change to take effect.
      • Verify the version of Go:
        • go version
  • Linux
    • Download and install:
      • cd
      • wget -c https://dl.google.com/go/go1.16.2.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
      • Add the location of the Go directory to the $PATH environment variable:
        • nano ~/.profile
          • export PATH=$PATH:/usr/local/go/bin
      • Load the new PATH environment variable into the current shell session:
        • source ~/.profile
      • Verify the version of Go:
        • go version

Testing:
  • Example 1 - Hello World
hello/hello.go
// A "Hello World" program that prints a greeting with the current time.
package main

import (
"fmt"
"time"
)

// greeting returns a pleasant, semi-useful greeting.
func greeting() string {
return "Hello world, the time is: " + time.Now().String()
}

func main() {
fmt.Println(greeting())
}
  • Configure the environment:
    • export GOPATH=/home/ubuntu/go
    • export GO111MODULE=auto
    • export GOROOT=/usr/local/go
  • Run the Hello World example:
    • cd $GOPATH
    • cd hello
    • go run hello.go

  • Example 2 - Call a Lib Function
projectpath/main.go
package main

import (
"fmt"
"projectpath/lib"
)

func main() {
fmt.Println(lib.Test())
 
projectpath/lib/util.go
package lib

import "time"

func Test() string {
return time.Now().String()
}