Hello World!
The following is the Go version of the Hello World! program. Please type it and save it as hw.go
:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
If you are eager to execute hw.go
, type go run hw.go
in the same directory where you save it. The file can also be found in the ch01
directory of the GitHub repository of the book.
Each Go source code begins with a package declaration. In this case, the name of the package is main
, which has a special meaning in Go—autonomous Go programs should use the main
package. The import
keyword allows you to include functionality from existing packages. In our case, we only need some of the functionality of the fmt
package that belongs to the standard Go library, implementing formatted input and output with functions that are analogous to C’s printf()
and scanf()
. The next important thing if you are creating an executable application is a main()
function. Go considers this the entry point to the application, and it begins the execution of an application with the code found in the main()
function of the main
package.
hw.go
is a Go program that runs on its own. Two characteristics make hw.go
a source file that can generate an executable binary: the name of the package, which should be main
, and the presence of the main()
function—we discuss Go functions in more detail in the next subsection, but we will learn even more about functions and methods, which are functions attached to specific data types, in Chapter 6, Go Packages and Functions.
Introducing functions
Each Go function definition begins with the func
keyword, followed by its name, signature, and implementation. Apart from the main()
function, which has a special purpose, you can name the rest of your functions anything you want—there is a global Go rule that also applies to function and variable names and is valid for all packages except main: everything that begins with a lowercase letter is considered private and is accessible in the current package only. We will learn more about that rule in Chapter 6, Go Packages and Functions. The only exception to this rule is package names, which can begin with either lowercase or uppercase letters. Having said that, I am not aware of a Go package that begins with an uppercase letter!
You might now ask how functions are organized and delivered. Well, the answer is in packages—the next subsection sheds some light on that.
Introducing packages
Go programs are organized in packages—even the smallest Go program should be delivered as a package. The package
keyword helps you define the name of a new package, which can be anything you want, with just one exception: if you are creating an executable application and not just a package that will be shared by other applications or packages, you should name your package main
. You will learn more about developing Go packages in Chapter 6, Go Packages and Functions.
Packages can be used by other packages. In fact, reusing existing packages is a good practice that saves you from having to write lots of code or implement existing functionality from scratch.
The import
keyword is used for importing other Go packages into your Go programs to use some or all of their functionality. A Go package can either be a part of the rich Standard Go library or come from an external source. Packages of the standard Go library are imported by name, for example, import
"os"
to use the os
package, whereas external packages like github.com/spf13/cobra
are imported using their full URLs: import "github.com/spf13/cobra"
.