Getting started
In this tutorial, you'll get a brief introduction to Go programming. Along the way, you will:
- Install Go (if you haven't already).
- Write some simple "Hello, world" code.
- Use the
gocommand to run your code. - Use the Go package discovery tool to find packages you can use in your own code.
- Call functions of an external module.
Prerequisites
- Some programming experience. The code here is pretty simple, but it helps to know something about functions.
- A tool to edit your code. Any text editor you have will work fine. Most text editors have good support for Go. The most popular are VSCode (free), GoLand (paid), and Vim (free).
- A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.
Install Go
Follow the Download and install steps.
Write some code
Get started with Hello, World:
-
Open a command prompt and
cdto your home directory.-
Linux or Mac:
cd -
Windows:
cd %HOMEPATH%
-
-
Create a
hellodirectory for your first Go source code: For example, use the following commands:mkdir hello cd hello -
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.
To enable dependency tracking for your code by creating a go.mod file, run the [
go mod init]command, giving it the name of the module your code will be in. The name is the module's module path.In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be
github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module.For the purposes of this tutorial, just use
example/hello.go mod init example/helloExample output:
go: creating new go.mod: module example/hello -
In your text editor, create a file hello.go in which to write your code.
-
Paste the following code into your hello.go file and save the file.
package main import "fmt" func main() { fmt.Println("Hello, World!") }Explanation:
- Declares a
mainpackage (a package groups functions and is comprised of all files in the same directory). - Imports the
fmtpackage, which contains functions for formatted I/O, including printing to the console. This is part of Go's standard library. - Implements the
mainfunction, which executes by default when running themainpackage.
- Declares a
-
Run your code to see the greeting.
go run .Output:
Hello, World!The
go runcommand is one of manygocommands. Usego helpto explore others.
Call code in an external package
- Visit pkg.go.dev and search for a
quotepackage. - Locate and click the v1 version of the
rsc.io/quotepackage (you'll see “Other major versions” listed likersc.io/quote/v4). - From the Documentation section, pick a function to call—use the
Gofunction. - Note that this package is in the
rsc.io/quotemodule.
In your hello.go, modify it as follows:
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}-
Run:
go mod tidyExample output:
go: finding module for package rsc.io/quote go: found rsc.io/quote in rsc.io/quote v1.5.2 -
Then run your code:
go run .Output:
Don't communicate by sharing memory, share memory by communicating.
This shows that go mod tidy finds and downloads the rsc.io/quote module (version v1.5.2).
Write more code
With this quick introduction, you got Go installed and learned some basics. To write more code and continue learning, check out the Create a Go module tutorial.