This is a field guide to Go. Go is a compiled language that runs in the Go runtime system.

Go is made of tokens and whitespace. There are four kinds of tokens: identifiers, keywords, operators and punctuation, and literals. There are four kinds of whitespace: spaces (U+0020), horizontal tabs (U+0009), carriage returns (U+000D), and newlines (U+000A). Whitespace separates tokens. Newlines (and ends of files) may invoke implicit terminator punctuation.

This field guide shows Go’s tokens, language constructs, and syntax in a valid script with comments. Each feature has an expansible explanation tab. Most of these are linked to the relevant documentation page. These examples can be copied and pasted directly into code.

  1. // Line comment. // Line Comment

    This is a single-line comment tag. It tells the Go runtime system to disregard any further commands on this line.

    Commenting stops at the end of the line.

  2. /* // General Comment Opening Tag

    This is the opening tag for a general comment. Commenting will start until the closing tag occurs.

    General comments may be multi-line, single-line, or within a single line.

    General comments of length 0 act like spaces.

  3. General comment.
  4. */ // General Comment Closing Tag

    This is the closing tag for a general comment.

  5. package main // Load Main Package

    This statement loads the “main” package. It tells Go to load the package as an executable program.

    All Go programs and libraries must start with a single, unimported package. All other packages are imported transitively.

    “package main” is necessary for executable programs. It is optional for shared libraries. Other

  6. import ( // Factored import Declaration

    The import statement imports packages into the current package and links them to the base package (“package main”, in this case).

  7. "fmt" // Package Declaration

    This declaration specifies a specific package to import. In this case, it’s the “fmt” package, which implements formatted inputs and outputs.

    It is “factored,” which means that multiple package imports can occur in a single declaration. This is considered better style than multiple, unfactored import declarations.

  8. "math"
  9. )
  10. import "net" // Unfactored import Declaration

    This is an unfactored import declaration.

    Multiple unfactored important statements are considered worse style than a single, factored import statement.

  11. import "net/http" // Subdirectory Package Declaration

    This declaration imports only a part of the “net” package. The “http” package is a complete package in and of itself, though.

  12. func main() { // main Function Declaration

    The main function in the main package is the entry point for Go programs.

    Program execution begins by initializing the main package and invoking the main function.

  13. }
  14. func test01() { // Function Declaration

    This declaration defines a custom function.

    It binds a function identifier (its name) to a function definition.

    Since no return type is specified, the return statement isnide is optional.

  15. return // Terminating Statement

    This statement prevents execution of all statements that appear after it in the same block.

    Valid terminating statements are:

    A statement list ends in a terminating statement if the list is not empty and the final non-empty statement is terminating.

  16. }
  17. func test02() int { // Function Declaration< with Return Type

    This declaration defines a custom function that returns a value of type int.

  18. return 0 // Return Statement with Values

    This statement prevents execution of all statements that appear after it in the same block and provides one or more result values (in this case, “0”).

  19. }
  20. func test03() ( int, int ) { // Function Declaration< with Multiple Return Type

    This declaration defines a custom function that returns two values, both of type int.

  21. return 0, 1 // Return Statement with Values

    This statement prevents execution of all statements that appear after it in the same block and provides one or more result values (in this case, “0” and “1”).

    Multiple values are separated by commas.

  22. }
  23. func test04( x int, y int ) int { return 0 } // Function Declaration with Parameters

    This declaration defines a custom function that accepts two parameters, each of type int.

  24. func test05( x, y int ) int { return 0 } // Function Declaration with Shortened Parameters

    This declaration defines a custom function that accepts two parameters, each of type int.

    When two sequential parameters are the same type, only the last parameter’s type needs to be specified.

  25. //