Golang Base

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

Follow the Download and install steps.


Write some code

Get started with Hello, World:

  1. Open a command prompt and cd to your home directory.

    • Linux or Mac:

      cd
    • Windows:

      cd %HOMEPATH%
  2. Create a hello directory for your first Go source code: For example, use the following commands:

    mkdir hello
    cd hello
  3. 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/hello

    Example output:

    go: creating new go.mod: module example/hello
  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.

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }

    Explanation:

    • Declares a main package (a package groups functions and is comprised of all files in the same directory).
    • Imports the fmt package, which contains functions for formatted I/O, including printing to the console. This is part of Go's standard library.
    • Implements the main function, which executes by default when running the main package.
  6. Run your code to see the greeting.

    go run .

    Output:

    Hello, World!

    The go run command is one of many go commands. Use go help to explore others.


Call code in an external package

  1. Visit pkg.go.dev and search for a quote package.
  2. Locate and click the v1 version of the rsc.io/quote package (you'll see “Other major versions” listed like rsc.io/quote/v4).
  3. From the Documentation section, pick a function to call—use the Go function.
  4. Note that this package is in the rsc.io/quote module.

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 tidy

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


On this page