Back to Json Iterator

Getting Started

_content/doc/tutorial/getting-started.html

latest3.6 KB
Original Source

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 go command 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

Just use the Download and install steps.

Write some code

Get started with Hello, World.

  1. On Linux or Mac:

  2. For example, use the following commands:

  3. 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.

  4. In your text editor, create a file hello.go in which to write your code.

  5. Paste the following code into your hello.go file and save the file.

  6. Run your code to see the greeting.

Call code in an external package

When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.

  1. Make your printed message a little more interesting with a function from an external module.
  2. Visit pkg.go.dev and search for a "quote" package.
  3. In the search results, locate and click on the v1 of the rsc.io/quote package (it should be listed with the "Other major versions" of rsc.io/quote/v4).
  4. In the Documentation section, under Index , note the list of functions you can call from your code. You'll use the Go function.
  5. At the top of this page, note that package quote is included in the rsc.io/quote module.

You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. 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.

  1. In your Go code, import the rsc.io/quote package and add a call to its Go function.

After adding the highlighted lines, your code should include the following:

package main

import "fmt"import "rsc.io/quote"func main() {fmt.Println(quote.Go())}
  1. Go will add the quote module as a requirement, as well as a go.sum file for use in authenticating the module. For more, see Authenticating modules in the Go Modules Reference.

  2. Run your code to see the message generated by the function you're calling.

$ go run .
Don't communicate by sharing memory, share memory by communicating.

Notice that your code calls the Go function, printing a clever message about communication.

When you ran go mod tidy, it located and downloaded the rsc.io/quote module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.

Write more code

With this quick introduction, you got Go installed and learned some of the basics. To write some more code with another tutorial, take a look at Create a Go module.