Go is a compiled language. The Go toolchain converts a source program and the things it depends on into instructions in the native machine language of the computer.
These tools are accessed through a single command called go that has a number of subcommands. Like run subcommand, compiles the source code from one or more source files (.go files), links it with libraries, then runs the resulting executable file.
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}$ go run helloworld.go
To compile,
$ go build helloworld.go
This creates an executable binary file called helloworld, that can be run anytime without further processing. We can simple run this now,
$ ./helloworld
Hello, World
Go code is organized into packages. A package consists of one or more .go source files in a single directory that define what the package does.
Each source file begins with a package declaration, stating which package this file belongs to, followed by a list of other packages that it imports.
For instance, fmt package contains functions for printing formatted output and scanning input.
Package main is special. It defines a standalone executable program, not a library. Within package main, the function main is also special, it’s where execution of the program begins. Whatever main does is what the program does.
You must import exactly the packages you need. A program will not compile if there are missing imports or if there are unnecessary ones.
Go does not require semicolons at the ends of statements or declarations, except where two or more appear on the same line.
Go takes a strong stance on code formatting. The gofmt tool rewrites code into the standard format. And go tool’s fmt subcommand applies gofmt to all the files in the specified package, or the ones in the current directory by default.
Many text editors can be configured to run gofmt each time you save a file. A related tool, goimports, additionally manages the insertion and removal of import declarations as needed.
The os package provides functions and other values for dealing with the operating system in a platform-independent manner.
Command line arguments are available to a program in a variable named Args that is part of the os package. Thus, its name anywhere outside the os package, is os.Args.
The variable os.Args is a slice of strings. Slices are a fundamental notion in go.