Skip to content

How to start a new project in golang?

Illustration for New Go Project

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 with go mod tidy
  • Try a small exercise: make a map from an array in Go
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

One short email when a new calculator or article goes live. No spam, and you can unsubscribe in one click.