To compile and run a simple program, first choose a module path (we'll
use
example.com/user/hello
) and create a
go.mod
file that declares it:
$ mkdir hello # Alternatively, clone it if it already exists in version control.
$ cd hello
$ go mod init example.com/user/hello
go: creating new go.mod: module example.com/user/hello
$ cat go.mod
module example.com/user/hello
go 1.14
$
The first statement in a Go source file must be
package name
. Executable commands must always use package main
.
Next, create a file named hello.go
inside that directory
containing the following Go code:
package main
import "fmt"
func main() {
fmt.Println("Hello, world.")
}
Now you can build and install that program with the
go
tool:
$ go install example.com/user/hello
$
This command builds the hello
command, producing an
executable binary. It then installs that binary as
$HOME/go/bin/hello
(or, under Windows,
%USERPROFILE%\go\bin\hello.exe
).
The install directory is controlled by the GOPATH
and
GOBIN
environment variables. If GOBIN
is set, binaries are installed to that
directory. If GOPATH
is set, binaries are installed to
the bin
subdirectory of the first directory in the
GOPATH
list. Otherwise, binaries are installed to the
bin
subdirectory of the default
GOPATH
($HOME/go
or
%USERPROFILE%\go
).
You can use the go env
command to portably set the
default value for an environment variable for future
go
commands:
$ go env -w GOBIN=/somewhere/else/bin
$
To unset a variable previously set by go env -w
, use
go env -u
:
$ go env -u GOBIN
$
Commands like go install
apply within the context of the
module containing the current working directory. If the working
directory is not within the
example.com/user/hello
module,
go install
may fail.
For convenience, go
commands accept paths relative to the
working directory, and default to the package in the current working
directory if no other path is given. So in our working directory, the
following commands are all equivalent:
$ go install example.com/user/hello
$ go install .
$ go install
Next, let's run the program to ensure it works. For added convenience,
we'll add the install directory to our PATH
to make
running binaries easy:
# Windows users should consult https://github.com/golang/go/wiki/SettingGOPATH
# for setting %PATH%.
$ export PATH=$PATH:$(dirname $(go list -f '{{.Target}}' .))
$ hello
Hello, world.
$
If you're using a source control system, now would be a good time to
initialize a repository, add the files, and commit your first change.
Again, this step is optional: you do not need to use source control to
write Go code.
$ git init
Initialized empty Git repository in /home/user/hello/.git/
$ git add go.mod hello.go
$ git commit -m "initial commit"
[master (root-commit) 0b4507d] initial commit
1 file changed, 7 insertion(+)
create mode 100644 go.mod hello.go
$
The go
command locates the repository containing a given
module path by requesting a corresponding HTTPS URL and reading
metadata embedded in the HTML response (see
go help importpath
). Many hosting services already provide that metadata for
repositories containing Go code, so the easiest way to make your
module available for others to use is usually to make its module path
match the URL for the repository.