Key Concepts

Review core concepts you need to learn to master this subject

Go Comments

// one line comment /* this comment is on multiple lines and ends here */

Comments are useful for documentation in a Go file and are ignored by the compiler. There are two types of comments:

  • a single-lined comment is preceded by a double forward slash, //, and ends at the end of the line.
  • a multi-lined comment begins with /* followed by one or more lines of comments and ends with */

Go Documentation

// one line comment /* this comment is on multiple lines and ends here */

In Go, comments can be used as built-in documentation. To check the role of a function, in the command line, use the command go doc followed by a package or the function of a package. For example:

$ go doc fmt

To find more information about a package’s function:

$ go doc fmt.println

Import Multiple Packages

// one line comment /* this comment is on multiple lines and ends here */

To import multiple packages in a Go file, use the import keyword followed by the package name enclosed in double-quotes and repeat this statement for every imported package on its own line, or write a single import keyword to import multiple packages, one per line, in enclosed parentheses, (…).

import "fmt" import "math" import "time"

or

import ( "fmt" "math" "time" )

Go Compiler

// one line comment /* this comment is on multiple lines and ends here */

As a compiled language, Go does not run until its source file is processed through a separate software called a compiler to produce a final executable program. The Go compiler can be accessed on the command line via a generic command such as:

go <command> [arguments]

Packages in Go

// one line comment /* this comment is on multiple lines and ends here */

A Go package is a directory made up of a collection of Go source files that are compiled together. This collection of reusable code typically contains functions related to a specific topic or concept. To use code from a particular package, we simply import it into our Go source file.

For example, to import the fmt package which contains functions for formatting input and output strings, we type the keyword import followed by the package name.

import "fmt"

Running Files in Go

// one line comment /* this comment is on multiple lines and ends here */

The Go compiler can execute Go code from the source file without producing an executable file. Instead of build, use run. To do this, type the following in the command line:

$ go run exampleFile.go

Compile Go

// one line comment /* this comment is on multiple lines and ends here */

The Go compiler takes a Go source file with a .go extension, processes it and produces an executable file without any extension. To compile a Go source file, test.go, type at the command line:

$ go build test.go

This will produce an executable file, test. To run test, type in the command line:

$ ./test

Go Import Package

// one line comment /* this comment is on multiple lines and ends here */

To import a single package in a Go file, use the keyword import followed by the package name in double-quotes.

Learn Go: Introduction
Lesson 1 of 1
  1. 1
    Go, or Golang, is an open sourced programming language designed by three Google employees: [Robert Griesemer](https://translate.google.com/translate?sl=auto&tl=en&u=https%3A%2F%2Fde.wikipedia.org%2…
  2. 2
    When we write Go code, we’re writing it to be readable for ourselves and other developers. We’re able to make sense of the code and its intentions. Computers, on the other hand, do NOT understand G…
  3. 3
    Great, we were able to compile our program into an executable file that will always print out “Hello World”. If we want our program to run again, we don’t have to compile the program again, we simp…
  4. 4
    Now that we understand how to compile and run Go programs, let’s take a detailed look at the structure of Go’s program, specifically its packages: package main import “fmt” func main () { f…
  5. 5
    Continuing on with our program, we have: func main () { fmt.Println(“Hello World”) } There are a few things happening in our main function. We’re introduced to how functions (reusable blo…
  6. 6
    Previously, we imported a single package, fmt. But, we can import so many more! Go has an extensive list of packages that we can take advantage of. [Here’s a list of Go’s standard packages](https:…
  7. 7
    We can’t always be there in person to explain to the next developer (or even our future self) what our code does or what our intentions were when writing it. That’s where comments come in. Commen…
  8. 8
    Learning a new language like Go involves learning the accompanying rules and syntax. But, we don’t have to commit everything to memory! It’s ok to search things up, in fact, that’s what all good pr…
  9. 9
    Great job! You’ve just finished your first Go lesson! In this lesson you learned about: * What Go/Golang is. * What Go is used for. * Go’s compiler. * How to compile Go files into an executable….

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo