How to start a new project in golang?

Every Go project starts with a module. Here is how to create one, add your first file and run it. You need Go installed; check with go version.
Step 1: Create a folder and a module
mkdir carexample
cd carexample
go mod init example.com/carexample
This creates a go.mod file. It records the module name and the Go version, and later it will list your dependencies.
Step 2: Add main.go
Create a file called main.go in the same folder:
package main
import "fmt"
func main() {
fmt.Println("Hello, welcome to the cars example Go application!")
}
Note the capital P in Println. In Go, only names that start with a capital letter can be used from outside their package, so fmt.println would not compile.
Step 3: Run it
go run .
You should see: Hello, welcome to the cars example Go application!
Next steps
- Build a program file you can share:
go build - Add a library, for example
go get github.com/google/uuid, then tidy the module withgo mod tidy - Try a small exercise: make a map from an array in Go