Packages and imports
Real programs lean on more than one package at a time. Go's standard library ships with dozens of small, focused packages, and your own code will usually import a handful on every file.
When you import more than one, the idiomatic form is a parenthesised block:
import (
"fmt"
"strings"
)
This is the form you will see everywhere in Go code. It is equivalent to writing two separate import "fmt" and import "strings" lines, just tidier.
A program that uses two packages
Here is a full program that imports both fmt and strings and uses a function from each:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToUpper("hello, go"))
}
strings.ToUpper("hello, go") calls the ToUpper function defined in the strings package. Without the import "strings" line, the name strings has no meaning in this file, and strings.ToUpper is a compile error.
Exported versus unexported names
Go uses capitalisation to mark visibility. Names that start with an uppercase letter are exported out of a package i.e. other packages that import it can see and use them. Names that start with a lowercase letter are unexported i.e. only code in the same package can use them.
That is why you write strings.ToUpper, not strings.toUpper. In Go, changing the case changes the name. Exported names start with an uppercase letter; lowercase names stay inside their own package.
This is Go's entire access-control system. No public or private keywords, just a capitalisation convention enforced by the compiler.
Capitalisation is part of the name. strings.ToUpper and strings.toUpper are not two spellings of the same function. If you change the T to lowercase, the compiler looks for a different name and fails with undefined: strings.toUpper. Try it, then change it back to ToUpper.
Sorting imports, and gofmt
By convention, imports are sorted alphabetically, so "fmt" comes before "strings" in the block above. You do not need to sort them by hand: Go's built-in formatter does it for you.
gofmt is Go's official code formatter. It reads your source, reformats it according to the standard Go style (indentation, spacing, import order, blank lines), and writes the result back. The Go community treats its output as the one true formatting, so there are no style debates.
On your own machine you run it like this:
gofmt -w main.go
The -w flag writes the formatted output back to the file in place. Most editors also run gofmt automatically on save once you install the Go tooling, so in practice you rarely type the command by hand. The editor on this site does not run gofmt, so your formatting here is whatever you type.
Add a second line below the existing one that uses strings.ToLower to print "HELLO, GO" in lowercase.
HELLO, GO hello, go