This guide is based on the official “Getting started” tutorial.
In this guide we present a brief introduction to Go programming. You will:
- Write some simple “Hello, world” code
- Use the
go
command to run your code - Use the Go package discovery site to find packages you can use in your own code
- Call functions of an external module
Prerequisites
You should already have completed:
This guide is running using:
$ go version
go version go1.19.1 linux/amd64
Write some code
As with all play-with-go.dev guides, we start in our home directory:
$ pwd
/home/gopher
Create a hello directory for your first Go source code:
$ mkdir /home/gopher/hello
$ cd /home/gopher/hello
Create the file hello.go
in /home/gopher/hello
:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This is your first .go
file! In this code you:
- Declare a
main
package (a package is a way to group related code and concepts). - Import the popular
fmt
package, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go. - Implement a
main
function to print a message to the console. Amain
function executes by default when you run code in the file.
Run your code to see the greeting:
$ go run hello.go
Hello, World!
Call code in an external package
When you want your code to do something that might have been implemented by someone else, you can look for a package that has the functionality you need.
Let’s make your printed message a little more interesting by using a function from an another package.
- Visit pkg.go.dev and search for a “quote” package.
- Locate and click the
rsc.io/quote
package in search results (if you seersc.io/quote/v3
, ignore it for now). - On the Doc tab, under Index, note the list of functions you can call from your code. You will use the
Go
function. - At the top of this page, note that package quote is included in the
rsc.io/quote
module - On the Versions tab, note the list of available versions of the
rsc.io/quote
module.
Packages are published in modules – like rsc.io/quote
– where others can use them. Modules are improved with new
versions over time, and you can upgrade your code to use the improved versions. You can use
pkg.go.dev to discover published modules whose packages have functions you can use in your own
code.
Modules can contain multiple packages. In the case of the the rsc.io/quote
module, it contains just one package:
rsc.io/quote
.
Let’s import the rsc.io/quote
package and add a call to its Go
function:
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
To import packages from another module, your code must belong to a module. Go modules are identified by a go.mod
file
in a directory. A go.mod
file lists the specific modules and versions providing those packages. That file stays with
your code, including in your source code repository. We refer to hello
as the current development
module.
To create a module and its go.mod
file, run the go mod init
command, giving it the name of the module your
code will be in (here, just use hello
):
$ go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
Our main
package now belongs to the hello
module.
We need to declare a dependency on the rsc.io/quote
module, specifically version v1.5.2
:
$ go get rsc.io/quote@v1.5.2
go: downloading rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added rsc.io/quote v1.5.2
go: added rsc.io/sampler v1.3.0
go get
resolves and adds
dependencies to the current development module, hello
in our case.
Re-run your code to see the message generated by the function you’re calling.
$ go run hello.go
Don't communicate by sharing memory, share memory by communicating.
Notice that your code calls the Go function, printing a clever message about communication.
Conclusion
With this quick introduction, you learned some of the basics.
As a next step you might like to consider: