Install Golang with Homebrew:
1 2 | $ brew update $ brew install golang |
Setup the workspace for Golang
Go has a different approach of managing code, you’ll need to create a single Workspace for all your Go projects. For more information consult setup the workspace
-
First, you will need to install Go on your machine. Go to the official Go website https://golang.org/ and click on the “Download Go” button to download the latest version of Go. Follow the installation instructions to install Go on your system.
-
Go uses a simple and easy-to-learn syntax that is similar to C. Here is a basic Go program that prints “Hello, world!” to the console:
1 2 3 4 5 6 7
package main import "fmt" func main() { fmt.Println("Hello, world!") }
-
To run the program, save it to a file with a .go extension (e.g. hello.go) and then run the go run command:
1
go run hello.go
-
Go also includes a built-in testing framework that allows you to write unit tests for your code. To write a test, create a file with a
_test.go
suffix (e.g.hello_test.go
) and write a test function with the following signature:1
func TestXxx(*testing.T)
Where
Xxx
is the name of the function being tested, and*testing.T
is a pointer to atesting.T
struct. For example:1 2 3 4 5 6 7 8 9 10
package main import "testing" func TestHello(t *testing.T) { result := hello() if result != "Hello, world!" { t.Errorf("Unexpected result: %s", result) } }
-
To run the tests, use the go test command:
1
go test
This is just a basic introduction to Go. There is much more to learn about Go, including its built-in data types, control structures, and concurrency features. I recommend reading the official Go tutorial https://tour.golang.org/welcome/1 for a more comprehensive introduction to Go.