Let's look a Go program in more detail:
package main import "fmt" // this is a comment func main() { fmt.Println("Hello, World") }
In the above Go programs the first line says this:
package main
This is a package declaration, and every Go program must start with it.
Packages are Go's way of organizing and reusing code.
There are two types of Go programs: executables and libraries.
On the following line, we see this:
import "fmt"
The import keyword can include code from other packages.
The fmt
package, shorthand for format, implements formatting for input and output.
Notice that fmt is surrounded by double quotes.
The line that starts with // is known as a comment.
Comments are ignored by the Go compiler.
Go supports two different styles of comments:
After this, you see a function declaration:
func main() {
fmt.Println("Hello, World")
}
Functions are the building blocks of a Go program.
All functions start with the keyword func
followed by the name of the function.
The name main is special because it's the function that gets called when you execute the program.
The final piece of our program is:
fmt.Println("Hello, World")
We access another function inside of the fmt
package called Println.
Println means "print line."
You can find out more about a function by typing the following in your terminal:
godoc fmt Println