- Tutorial: Get started with Go
- Tutorial: Create a Go module
- Create a module
- Call your code from another module
- Return and handle an error
- Return a random greeting
- Return greetings for multiple people
- Add a test
- Compile and install the application
- Tutorial: Getting started with multi-module workspaces
- Tutorial: Accessing a relational database
- Tutorial: Developing a RESTful API with Go and Gin
- Tutorial: Getting started with generics
- Tutorial: Getting started with fuzzing
- Tutorial: Getting started with govulncheck
- Tutorial: Find and fix vulnerable dependencies with VS Code Go
- Tutorial: A Tour of Go
- The main documentation page outlines a lot of the learning journeys and tutorial paths
- See Effective Go for tips on writing clear, idiomatic Go code.
- Take A Tour of Go to learn the language proper.
- Visit the documentation page for a set of in-depth articles about the Go language and its libraries and tools.
- See the testing package documentation for more detail on testing.
- Go Maps in Action
- For more, see The blank identifier in Effective Go.
Before writing any code, if you are writing a module you need to create the directory which will host your module.
Run the go mod init command, giving it your module path -- here, use example.com/greetings. If you publish a module, this must be a path from which your module can be downloaded by Go tools. That would be your code's repository.
go mod init example.com/name_of_module
You go through the same motions as creating modules, and then declare a main package. In Go, code executed as an application must be in a main package.
package main
import (
// packages
)
func main() {
// do something using packages
}
In Go, the := operator is a shortcut for declaring and initializing a variable in one line (Go uses the value on the right to determine the variable's type). Taking the long way, you might have written this as:
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)
In Go, a function whose name starts with a capital letter can be called by a function not in the same package. This is known in Go as an exported name.
Go has something called slices
. These are like arrays, except their sizes
change dynamically in line with their contents.
When declaring a slice, you omit its size in the brackets, like this:
[]string.
This tells Go that the size of the array underlying the slice can be dynamically changed.
In Go, you initialize a map with the following syntax:
make(map[key-type]value-type)
make(map[string]int)
Values can be added to the map, using either map literals:
// this also creates the map
commits := map[string]int{
"rsc": 3711,
"r": 2138,
"gri": 1908,
"adg": 912,
}
Or separate statements:
m = make(map[string]int)
m["route"] = 66
for _, name := range names {
message, err := Hello(name)
if err != nil {
return nil, err
}
// In the map, associate the retrieved message with
// the name.
messages[name] = message
}
The range
returns two values: the index of the current item in the
loop and a copy of the item's value. You don't need the index, so you use the Go
blank identifier (an underscore) to ignore it.