Next: Command-Line Arguments, Up: Tutorial [Index]
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Listing 1.1: gopl.io/ch1/helloworld.go
(princ "Hello, world!")
Listing 1.2: lisp/ch1/helloworld.lisp
write is the general entry point to the Lisp printer.
prin1 produces output suitable for input to read.
princ is just like prin1 except that the output has no escape characters.
princ is intended to look good to
people, while output from prin1 is intended to be acceptable to read.
print is just like prin1 except that the printed representation of
‘object’ is preceded by a newline and followed by a space.
pprint is just like print except that the trailing space is omitted and
‘object’ is printed with the ‘*print-pretty*’ flag non-nil to produce pretty
output.
The Go toolchain converts a source program and the things it depends on into
instructions in the native machine language of a computer. These tools are
accessed through a single command called go that has a number of subcommands.
go runThe simplest of these subcommands is go run, which compiles the source code
from one or more source files whose names end in .go, links it with
libraries, then runs the resulting executable file.
$ go run helloworld.go => Hello, World!
go buildIf the program is more than a one-shot experiment, it’s likely that you would
want to compile it once and save the compiled result for later use. That is
done with go build:
$ go build helloworld.go $ ./helloworld => Hello, World!
This creates an executable binary file called helloworld that can be run any
time without further processing.
Go code is organized into packages, which are similar to libraries or modules in other languages.
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 @texinfo:@dfn{package declaration}, here
package main, that states which package the file belongs to, followed by a
list of other packages that it imports, and then the declarations of the
program that are stored in that file.
The Go standard library has over 100 packages for common tasks like
fmt Package and PrintlnFor instance, the fmt package contains functions for printing formatted
output and scanning input. Println is one of the basic output functions in
fmt; it prints one or more values, separated by spaces, with a newline
character at the end so that the values appear as a single line of output.
mainPackage 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. Of course, main will normally call upon functions in other
packages to do much of the work, such as the function fmt.Println.
Import DeclarationWe must tell the compiler what packages are needed by this source file; that’s
the role of the import declaration that follows the package declaration. The
“hello, world” program uses only one function from one other package, but most
programs will import more packages.
You must import exactly the packages you need. A program will not compile if there are missing imports or if there are unnecessary ones. This strict requirement prevents references to unused packages from accumulating as programs evolve.
The import declarations must follow the package declaration. After that, a
program consists of the declarations of functions, variables, constants, and
types (introduced by the keywords func, var, const, and type); for the
most part, the order of declarations does not matter. This program is about as
short as possible since it declares only one function, which in turn calls only
one other function. To save space, we will sometimes not show the package and
import declarations when presenting examples, but they are in the source file
and must be there to compile the code.
A function declaration consists of the keyword func, the name of the
function, a parameter list (empty for main), a result list (also empty here),
and the body of the function—the statements that define what it
does—–enclosed in braces. We’ll take a closer look at functions in Chapter 5.
Go does not require semicolons at the ends of statements or declarations, except where two or more appear on the same line. In effect, newlines following certain tokens are converted into semicolons, so where newlines are placed matters to proper parsing of Go code.
func declaration, not on a line by itself, and in the
expression ‘x + y’, a newline is permitted after but not before the ‘+’
operator.
gofmt Tool and fmt SubcommandGo takes a strong stance on code formatting. The gofmt tool rewrites code
into the standard format, and the go tool’s fmt subcommand applies gofmt to
all the files in the specified package, or the ones in the current directory by
default.
All Go source files in the book have been run through gofmt, and you should
get into the habit of doing the same for your own code. Declaring a standard
format by fiat eliminates a lot of pointless debate about trivia and, more
importantly, enables a variety of automated source code transformations that
would be infeasible if arbitrary formatting were allowed.
Many text editors can be configured to run gofmt each time you save a file,
so that your source code is always properly formatted.
goimportsA related tool, goimports, additionally manages the insertion and removal of
import declarations as needed. It is not part of the standard distribution
but you can obtain it with this command:
$ go get golang.org/x/tools/cmd/goimports
Next: Command-Line Arguments, Up: Tutorial [Index]