Go Tool: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:
</syntaxhighlight>
</syntaxhighlight>
==<tt>build</tt>==
==<tt>build</tt>==
The <code>build</code> command compiles one, possible more packages and optionally builds an executable.
The <code>build</code> command compiles one or more packages and builds the executable if the [[Go_Packages#The_main_Package|package <code>main</code>]] is among the compiled packages.
 
 
 
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
cd ${PROJECT_DIR}
cd ${PROJECT_DIR}
Line 21: Line 24:


To build in [[Go_Language_Modularization#Module-Aware_or_GOPATH_Mode|GOPATH mode]], see: {{Internal|Go_Packages#Building_with_go_build|Building Packages with <tt>go build</tt>}}
To build in [[Go_Language_Modularization#Module-Aware_or_GOPATH_Mode|GOPATH mode]], see: {{Internal|Go_Packages#Building_with_go_build|Building Packages with <tt>go build</tt>}}
==<tt>install</tt>==
==<tt>install</tt>==
* [[Go_Packages#Publishing_Locally|Go Packages &#124; Publishing Locally]]
* [[Go_Packages#Publishing_Locally|Go Packages &#124; Publishing Locally]]

Revision as of 01:54, 8 September 2023

Internal

Overview

go is a command line tool with multiple uses: package manager, build tool and test driver. go manage packages in workspaces, query metadata about packages, print documentation, build, format, download, test, etc.

Commands

Help

go help <command>
go help build

build

The build command compiles one or more packages and builds the executable if the package main is among the compiled packages.


cd ${PROJECT_DIR}
go build ./src/main/main.go

The command will create an executable named after the first source file, in this case main, and will place it by default in the ${PROJECT_DIR} directory.

To build in GOPATH mode, see:

Building Packages with go build

install

doc

The doc command prints documentation for a package.

fmt

The fmt ("format") command formats source code files.

get

The get command downloads packages and installs them.

list

The list command lists all installed packages.

run

The run command compiles .go files and runs the executable.

cd $PROJECT_DIR
go run ./src/main/main.go

test

The test command runs tests. For more details, see:

Go Testing

env

The env command prints the effective values of the environment variables relevant to the tool chain.

go env GOOS GOARCH GOPATH

get

The get command retrieve and update packages.

TODO Addison-Wesley The Go Programming Language Section 10.7.2