================ CODE SNIPPETS ================ TITLE: Basic Golang Program DESCRIPTION: A minimal Go program with a main function, serving as a starting point for a Go project. It demonstrates the basic structure of a Go executable. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/install_relative_gobin_fail.txt#_snippet_2 LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Go Project Setup and Build Commands DESCRIPTION: This snippet demonstrates setting environment variables for Go modules, creating a vendor directory, copying Go source files, creating a symbolic link, and then executing 'go list', 'go run', 'go build', and 'go install' commands. It verifies the project root and ensures build commands succeed. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_vendor_issue14054.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=off mkdir $WORK/tmp/gopath/src/dir1/vendor/v cp p.go $WORK/tmp/gopath/src/dir1/p.go cp v.go $WORK/tmp/gopath/src/dir1/vendor/v/v.go symlink $WORK/tmp/symdir1 -> $WORK/tmp/gopath/src/dir1 env GOPATH=$WORK/tmp/gopath cd $WORK/tmp/symdir1 go list -f '{{.Root}}' . # stdout '^'$WORK/tmp/gopath'$' # All of these should succeed, not die in vendor-handling code. go run p.go & go build & go install & wait ``` -------------------------------- TITLE: Go Project Setup and Execution DESCRIPTION: This snippet outlines the shell commands to set up a Go project environment. It involves creating directories, copying Go source files, creating a symbolic link, setting the GOPATH, and then executing Go commands like `go list`, `go run`, `go build`, and `go install` concurrently. The `go list` command is used to verify the project root. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_internal.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=off mkdir $WORK/tmp/gopath/src/dir1/internal/v cp p.go $WORK/tmp/gopath/src/dir1/p.go cp v.go $WORK/tmp/gopath/src/dir1/internal/v/v.go symlink $WORK/tmp/symdir1 -> $WORK/tmp/gopath/src/dir1 env GOPATH=$WORK/tmp/gopath cd $WORK/tmp/symdir1 go list -f '{{.Root}}' . stdout '^'$WORK/tmp/gopath'$' # All of these should succeed, not die in internal-handling code. go run p.go & go build & go install & wait ``` -------------------------------- TITLE: Go Module Initialization and Package Installation DESCRIPTION: Demonstrates initializing a Go module and installing a specific package version. This involves creating a go.mod file and using `go get` to fetch dependencies. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_gopkg_unstable.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on cp go.mod.empty go.mod go get gopkg.in/dummy.v2-unstable ``` -------------------------------- TITLE: Go Module and Installation Example DESCRIPTION: This snippet demonstrates setting the GO111MODULE environment variable to 'on' for module support. It also shows examples of `go get` for fetching packages and `go install` for building and installing packages. The `go.mod` file defines the module path, and `main.go` shows a basic structure with an import statement. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_import_v1suffix.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on ! go get example.com/invalidpath/v1 ! go install . ``` LANGUAGE: go CODE: ``` -- go.mod -- module example.com -- main.go -- package main import _ "example.com/invalidpath/v1" func main() {} ``` -------------------------------- TITLE: Go Project Setup and Package Listing Example DESCRIPTION: This snippet illustrates a common Go development workflow: creating a project directory, setting the GOPATH, and then using `go list` to verify the package root. It simulates a build environment setup. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=off mkdir $WORK/tmp/src symlink $WORK/tmp/src/dir1 -> $WORK/tmp cp p.go $WORK/tmp/src/dir1/p.go env GOPATH=$WORK/tmp go list -f '{{.Root}}' dir1 stdout '^'$WORK/tmp'$' ``` LANGUAGE: go CODE: ``` package p ``` -------------------------------- TITLE: Basic Go Project File Setup DESCRIPTION: Provides examples of creating essential project files like `go.mod` and a source file (`x.go`) that imports a package. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_gopkg_unstable.txt#_snippet_3 LANGUAGE: go CODE: ``` -- go.mod.empty -- module m -- x.go.txt -- package x import _ "gopkg.in/dummy.v2-unstable" ``` -------------------------------- TITLE: Building a Go Project DESCRIPTION: Shows the command to build a Go project. It includes an example of the expected output when no modules are found, guiding the user to initialize their workspace. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_build_no_modules.txt#_snippet_1 LANGUAGE: shell CODE: ``` go build . stderr 'go: no modules were found in the current workspace; see \'go help work\'' ``` -------------------------------- TITLE: Main Package (`p.go`) DESCRIPTION: This Go file defines the `main` package and imports a local package `dir1/internal/v`. It serves as an entry point for the Go program execution example. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_internal.txt#_snippet_1 LANGUAGE: go CODE: ``` package main import _ `dir1/internal/v` func main() {} ``` -------------------------------- TITLE: Go Install and Run DESCRIPTION: Commands to install Go executables and run Go programs. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/version_replace.txt#_snippet_1 LANGUAGE: go CODE: ``` go install example.com/printversion ``` LANGUAGE: go CODE: ``` go run example.com/printversion ``` -------------------------------- TITLE: Initialize Go Module and Install Packages DESCRIPTION: This snippet demonstrates how to create a new Go module and install packages using the go command. It includes setting up a temporary directory for packages and installing the standard library with the race detector enabled. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/test_race_install.txt#_snippet_0 LANGUAGE: shell CODE: ``` mkdir $WORKDIR/tmp/pkg go install -race -pkgdir=$WORKDIR/tmp/pkg std ``` -------------------------------- TITLE: Git Environment Setup and Initialization DESCRIPTION: Sets Git author and committer information and initializes a new Git repository. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/hello.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GIT_AUTHOR_NAME=bwk env GIT_AUTHOR_EMAIL=bwk env GIT_COMMITTER_NAME='Russ Cox' env GIT_COMMITTER_EMAIL='rsc@golang.org' git init ``` -------------------------------- TITLE: Go Project Configuration and CGO Installation DESCRIPTION: This snippet demonstrates setting the GO111MODULE environment variable to 'on' to enable Go modules, skipping CGO compilation if not needed, and installing the rsc.io/CGO package using 'go get'. It also shows how to stop the build process if a short build is intended. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_case_cgo.txt#_snippet_0 LANGUAGE: shell CODE: ``` #!cgo skip env GO111MODULE=on go get rsc.io/CGO [short] stop go build rsc.io/CGO ``` -------------------------------- TITLE: Cross-Compilation Setup and Installation DESCRIPTION: Demonstrates setting environment variables for cross-compilation (GOARCH, GOOS) and performing installations with both implicit and explicit GOBIN paths. It highlights the resulting binary locations and checks for their existence. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/install_cross_gobin.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=off cd mycmd go build mycmd # cross-compile install with implicit GOBIN=$GOPATH/bin can make subdirectory env GOARCH=386 [GOARCH:386] env GOARCH=amd64 env GOOS=linux go install mycmd exists $GOPATH/bin/linux_$GOARCH/mycmd # cross-compile install with explicit GOBIN cannot make subdirectory env GOBIN=$WORK/bin ! go install mycmd ! exists $GOBIN/linux_$GOARCH # The install directory for a cross-compiled standard command should include GOARCH. go list -f '{{.Target}}' cmd/pack stdout ${GOROOT}[/\\]pkg[/\\]tool[/\]${GOOS}_${GOARCH}[/\]pack$ ``` -------------------------------- TITLE: Git Initialization and Author Setup DESCRIPTION: Sets up Git author and committer information and initializes a new Git repository. This is a foundational step for version control. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/prefixtagtests.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GIT_AUTHOR_NAME='Jay Conrod' env GIT_AUTHOR_EMAIL='jayconrod@google.com' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL git init ``` -------------------------------- TITLE: Install Tools with Wildcards DESCRIPTION: Installs multiple tools within a specified directory path using a wildcard. This allows for bulk installation of tools defined in subdirectories. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_tool.txt#_snippet_3 LANGUAGE: shell CODE: ``` go get -tool ./cmd/... cmp go.mod go.mod.wildcard ``` -------------------------------- TITLE: Go Module Initialization and Build DESCRIPTION: Demonstrates initializing a Go module using 'go mod init' and building the project with 'go build'. This is a standard workflow for Go projects. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_string_alias.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on go mod init golang.org/issue/27584 go build . ``` -------------------------------- TITLE: Golang Installation with GOBIN DESCRIPTION: Demonstrates the correct usage of 'go install' and the error encountered when GOBIN is not an absolute path. This snippet highlights environment setup for Go projects. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/install_relative_gobin_fail.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GOBIN=. ! go install stderr 'cannot install, GOBIN must be an absolute path' ``` -------------------------------- TITLE: Go Main Package DESCRIPTION: A simple Go program that imports a package named 'v'. This serves as the main entry point for the demonstration. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_vendor_issue14054.txt#_snippet_1 LANGUAGE: go CODE: ``` package main import _ `v` func main () {} ``` -------------------------------- TITLE: helloworld.go Example DESCRIPTION: A simple Go program that prints 'hello world' to standard output. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/gopath_install.txt#_snippet_7 LANGUAGE: go CODE: ``` package main func main() { println("hello world") } ``` -------------------------------- TITLE: Go Module Configuration and Usage DESCRIPTION: Provides example go.mod and go.go files demonstrating a basic Go module setup and import. This serves as a minimal reproducible example for module functionality. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_getmode_vendor.txt#_snippet_3 LANGUAGE: go CODE: ``` -- go.mod -- module x go 1.16 -- x.go -- package x import _ "rsc.io/quote" ``` -------------------------------- TITLE: Go Module Resolution with Symlinks DESCRIPTION: This snippet demonstrates setting up a Go module environment using symlinks to simulate module resolution. It covers creating symlinks for go.mod, go.sum, and package files, then uses 'go env', 'go list', and 'go get' to verify the setup. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_symlink.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on [!symlink] skip # 'go get' should resolve modules of imported packages. go get go list -deps -f '{{.Module}}' . stdout golang.org/x/text go get ./subpkg go list -deps -f '{{.Module}}' ./subpkg stdout golang.org/x/text # Create a copy of the module using symlinks in src/links. mkdir links symlink links/go.mod -> $GOPATH/src/go.mod symlink links/go.sum -> $GOPATH/src/go.sum symlink links/issue.go -> $GOPATH/src/issue.go mkdir links/subpkg symlink links/subpkg/issue.go -> $GOPATH/src/subpkg/issue.go # We should see the copy as a valid module root. cd links go env GOMOD stdout links[/\]go.mod go list -m stdout golang.org/issue/28107 # The symlink-based copy should contain the same packages # and have the same dependencies as the original. go list -deps -f '{{.Module}}' . stdout golang.org/x/text go list -deps -f '{{.Module}}' ./subpkg stdout golang.org/x/text ``` -------------------------------- TITLE: Go Module and Testing Setup DESCRIPTION: Sets up a Go module environment and demonstrates testing scenarios. It includes creating directories, copying test files, and executing Go commands to inspect dependencies and package visibility. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_test_files.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on cd foo # Testing an explicit source file should use the same import visibility as the # package in the same directory. go list -test -deps go list -test -deps foo_test.go # If the file is inside the main module's vendor directory, it should have # visibility based on the vendor-relative import path. mkdir vendor/example.com/foo cp foo_test.go vendor/example.com/foo go list -test -deps vendor/example.com/foo/foo_test.go # If the file is outside the main module entirely, it should be treated as outside. cp foo_test.go ../foo_test.go ! go list -test -deps ../foo_test.go stderr 'use of internal package' ``` -------------------------------- TITLE: Install Specific Tool Version DESCRIPTION: Installs a specific version of a tool using 'go get -tool'. This operation updates the go.mod file to reflect the required tool dependency. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_tool.txt#_snippet_0 LANGUAGE: shell CODE: ``` go get -tool example.com/tools/cmd/hello@v1.0.0 cmp go.mod go.mod.want ``` -------------------------------- TITLE: Go Build with Cache Setup DESCRIPTION: Configures the Go build cache and builds the 'runtime/cgo' package to ensure necessary dependencies are available for subsequent builds. This step is crucial for avoiding import errors. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/build_dash_x.txt#_snippet_0 LANGUAGE: shell CODE: ``` mkdir $WORK/tmp/cache env GOCACHE=$WORK/tmp/cache go build runtime/cgo ``` -------------------------------- TITLE: Go Module Origin File DESCRIPTION: Provides the content of the original go.mod file used in the examples. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_deprecate_install.txt#_snippet_4 LANGUAGE: go CODE: ``` -- go.mod.orig -- module m go 1.17 ``` -------------------------------- TITLE: Go Assembly Symbol Naming Example DESCRIPTION: Demonstrates how Go's assembly linker prefixes symbol names starting with a period with the current object file's package path. For example, `·Int` in `math/rand` becomes `math/rand.Int`. SOURCE: https://github.com/golang/go/blob/go1.25.0/doc/asm.html#_snippet_10 LANGUAGE: go CODE: ``` // In math/rand package assembly file: // Symbol `·Int` is resolved by the linker to `math/rand.Int` ``` -------------------------------- TITLE: Go Module Downgrade Test Setup DESCRIPTION: This snippet outlines the initial setup and commands executed to test the module downgrade scenario. It includes copying the go.mod file, tidying modules, comparing files, and performing a specific module get operation. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_downadd_indirect.txt#_snippet_0 LANGUAGE: shell CODE: ``` cp go.mod go.mod.orig go mod tidy cmp go.mod.orig go.mod go get example.com/d@v0.1.0 go list -m all stdout '^example.com/b v0.1.0 ' stdout '^example.com/c v0.1.0 ' stdout '^example.com/d v0.1.0 ' ``` -------------------------------- TITLE: Module Configuration for 'q' DESCRIPTION: Basic module configuration for 'example.com/q', specifying the module name and Go version. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_12 LANGUAGE: go CODE: ``` module example.com/q go 1.21 ``` -------------------------------- TITLE: Example Go Main File DESCRIPTION: A minimal Go program with an empty main function, suitable for initial project setup. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/mainonly.txt#_snippet_3 LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Getting Modules with Exclusions DESCRIPTION: Illustrates how 'go get' behaves when attempting to retrieve module versions that are excluded in the go.mod file. Includes examples of getting a specific excluded version and a version range. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_query_exclude.txt#_snippet_1 LANGUAGE: shell CODE: ``` # get excluded version cp go.exclude.mod go.exclude.mod.orig ! go get -modfile=go.exclude.mod rsc.io/quote@v1.5.0 stderr '^go: rsc.io/quote@v1.5.0: excluded by go.mod$' # get non-excluded version cp go.exclude.mod.orig go.exclude.mod go get -modfile=go.exclude.mod rsc.io/quote@v1.5.1 stderr 'rsc.io/quote v1.5.1' # get query with excluded version cp go.exclude.mod.orig go.exclude.mod go get -modfile=go.exclude.mod rsc.io/quote@>=v1.5 go list -modfile=go.exclude.mod -m ...quote stdout 'rsc.io/quote v1.5.[1-9]' ``` -------------------------------- TITLE: README File DESCRIPTION: Describes the project's evolution, specifically mentioning the addition of a subpackage in a later version. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/mod/example.net_pkgadded_v1.1.0.txt#_snippet_3 LANGUAGE: text CODE: ``` We will add the package example.net/pkgadded/subpkg in v1.2.0. ``` -------------------------------- TITLE: Module Configuration for Example 'a' DESCRIPTION: Specifies the module name, Go version, and dependencies for the 'example.com/a' module. It also includes a local replacement for 'example.com/p'. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_9 LANGUAGE: go CODE: ``` module example.com/a go 1.21 require example.com/p v1.0.0 replace example.com/p v1.0.0 => ../p ``` -------------------------------- TITLE: Module Configuration for Example 'b' DESCRIPTION: Defines the module name, Go version, and dependencies for 'example.com/b'. It includes a local replacement for 'example.com/q'. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_10 LANGUAGE: go CODE: ``` module example.com/b go 1.21 require example.com/q v1.0.0 replace example.com/q v1.0.0 => ../q ``` -------------------------------- TITLE: Running 'go get' for Main Package in Main Module DESCRIPTION: Explains that 'go get' does not print a warning for a main package within the main module, as the intent is likely to update dependencies. 'go install' is noted as an alternative for other use cases. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_deprecate_install.txt#_snippet_3 LANGUAGE: bash CODE: ``` cp go.mod.orig go.mod # 'go get' should not print a warning for a main package inside the main module. # The intent is most likely to update the dependencies of that package. # 'go install' would be used otherwise. go get m ! stderr . cp go.mod.orig go.mod ``` -------------------------------- TITLE: Go Project Setup with GOPATH and Vendor DESCRIPTION: This snippet outlines the steps to set up a Go project using GOPATH and the vendor directory. It includes creating directories, copying files, and using symlinks to simulate a vendored dependency structure. The `go list ./...` command is used to display the modules within the project. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_vendor_issue15201.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=off mkdir $WORK/tmp/gopath/src/x/y/_vendor/src/x symlink $WORK/tmp/gopath/src/x/y/_vendor/src/x/y -> ../../.. mkdir $WORK/tmp/gopath/src/x/y/_vendor/src/x/y/w cp w.go $WORK/tmp/gopath/src/x/y/w/w.go symlink $WORK/tmp/gopath/src/x/y/w/vendor -> ../_vendor/src mkdir $WORK/tmp/gopath/src/x/y/_vendor/src/x/y/z cp z.go $WORK/tmp/gopath/src/x/y/z/z.go env GOPATH=$WORK/tmp/gopath/src/x/y/_vendor${:}$WORK/tmp/gopath cd $WORK/tmp/gopath/src go list ./... ``` -------------------------------- TITLE: Go Get: Transitive Dependency Resolution Failure DESCRIPTION: Shows a scenario where 'go get' fails due to a version constraint that prevents resolving a transitive dependency. The example includes the go.mod and m.go files, the 'go get' command, and the expected error output. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_downgrade_missing.txt#_snippet_2 LANGUAGE: shell CODE: ``` # If we need to resolve a transitive dependency of a package, # and another argument constrains away the version that provides that # package, then 'go get' should fail with a useful error message. go get example.net/pkgadded@v1.0.0 . ! go list -deps -mod=readonly . stderr '^m.go:3:8: cannot find module providing package example.net/pkgadded/subpkg: ' ``` LANGUAGE: go CODE: ``` -- go.mod -- module example.com/m go 1.16 require example.net/pkgadded v1.2.0 -- m.go -- package m import _ "example.net/pkgadded/subpkg" ``` -------------------------------- TITLE: Go Build and Execution Examples DESCRIPTION: Illustrates various ways to build Go programs, specify output names and directories, and execute the compiled binaries. It also shows how to handle conditional compilation for different operating systems. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/build_output.txt#_snippet_0 LANGUAGE: shell CODE: ``` go build x.go exists -exec x$GOEXE rm x$GOEXE ! exists x$NONEXE ``` LANGUAGE: shell CODE: ``` go build -o myprog x.go ! exists x ! exists x.exe exists -exec myprog ! exists myprogr.exe ``` LANGUAGE: shell CODE: ``` ! exists bin go build -o bin/x x.go exists -exec bin/x rm bin ``` LANGUAGE: shell CODE: ``` ! exists bin go build -o bin/ x.go exists -exec bin/x$GOEXE rm bin ``` LANGUAGE: shell CODE: ``` ! exists bin mkdir bin go build -o bin x.go exists -exec bin/x$GOEXE rm bin ``` LANGUAGE: shell CODE: ``` go build p.go ! exists p ! exists p.a ! exists p.o ! exists p.exe ``` LANGUAGE: shell CODE: ``` go build -o p.a p.go exists p.a exec $GOBIN/isarchive p.a ``` LANGUAGE: shell CODE: ``` go build cmd/gofmt exists -exec gofmt$GOEXE rm gofmt$GOEXE ! exists gofmt$NONEXE ``` LANGUAGE: shell CODE: ``` go build -o mygofmt cmd/gofmt exists -exec mygofmt ! exists mygofmt.exe ! exists gofmt ! exists gofmt.exe ``` LANGUAGE: shell CODE: ``` go build sync/atomic ! exists atomic ! exists atomic.exe ``` LANGUAGE: shell CODE: ``` go build -o myatomic.a sync/atomic exists myatomic.a exec $GOBIN/isarchive myatomic.a ! exists atomic ! exists atomic.a ! exists atomic.exe ``` LANGUAGE: shell CODE: ``` ! go build -o whatever cmd/gofmt sync/atomic stderr 'multiple packages' ``` -------------------------------- TITLE: Go Install Error Reporting for Conflicting Modules DESCRIPTION: This example shows how 'go install' reports an error when attempting to install a package that is hidden by another package with the same import path in a preceding GOPATH entry. The error message includes the conflicting directory. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_shadow.txt#_snippet_1 LANGUAGE: shell CODE: ``` # The error for go install should mention the conflicting directory. ! go install -n ./shadow/root2/src/foo stderr 'go: no install location for '$WORK'(\|\|/)?gopath(\|\|/)src(\|\|/)shadow(\|\|/)root2(\|\|/)src(\|\|/)foo: hidden by '$WORK'(\|\|/)?gopath(\|\|/)src(\|\|/)shadow(\|\|/)root1(\|\|/)src(\|\|/)foo' ``` -------------------------------- TITLE: List Import Path for Standard Library Package (GO111MODULE=off) DESCRIPTION: This snippet shows how to use 'go list' to get the import path of a standard library package when GO111MODULE is set to 'off'. It targets the 'math' package within the Go installation directory. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_list_dir.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=off go list -f '{{.ImportPath}}' $GOROOT/src/math stdout ^math$ ``` -------------------------------- TITLE: Module Configuration for 'p' DESCRIPTION: Basic module configuration for 'example.com/p', specifying the module name and Go version. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_11 LANGUAGE: go CODE: ``` module example.com/p go 1.21 ``` -------------------------------- TITLE: Go Test2json Tool Installation and Verification DESCRIPTION: Demonstrates the installation of the 'test2json' tool and verifies its presence and execution. It checks if the tool is available in the expected location and confirms its output format. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/tool_build_as_needed.txt#_snippet_1 LANGUAGE: sh CODE: ``` # GOROOT without test2json tool builds and runs it as needed go env GOROOT ! exists $TOOLDIR/test2json go tool test2json stdout '{"Action":"start"}' ! exists $TOOLDIR/test2json$GOEXE go tool -n test2json ! stdout $NEWTOOLDIR${/}test2json$GOEXE # GOROOT with test2json uses the test2json in the GOROOT go install cmd/test2json exists $TOOLDIR/test2json$GOEXE go tool test2json stdout '{"Action":"start"}' go tool -n test2json stdout $NEWTOOLDIR${/}test2json$GOEXE ``` -------------------------------- TITLE: Limit Upgrades with 'go get -u' DESCRIPTION: This example demonstrates the use of the '-u' flag with 'go get' to limit upgrades to specific modules. It upgrades 'rsc.io/quote' and 'rsc.io/sampler' to specified versions and then verifies the outcome. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_downgrade.txt#_snippet_3 LANGUAGE: shell CODE: ``` # go get -u args should limit upgrades cp go.mod.empty go.mod go get -u rsc.io/quote@v1.4.0 rsc.io/sampler@v1.0.0 go list -m all stdout 'rsc.io/quote v1.4.0' stdout 'rsc.io/sampler v1.0.0' ! stdout golang.org/x/text ``` -------------------------------- TITLE: Git Initialization and Basic Commits DESCRIPTION: Sets up Git author information, initializes a repository, adds files, makes an initial commit, renames the main branch, and tags the first release. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/semver-branch.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GIT_AUTHOR_NAME='Bryan C. Mills' env GIT_AUTHOR_EMAIL='bcmills@google.com' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL git init at 2022-02-02T14:15:21-05:00 git add pkg go.mod git commit -a -m 'pkg: add empty package' git branch -m main git tag 'v0.1.0' ``` -------------------------------- TITLE: Removing a Dependency with 'go get' DESCRIPTION: This example illustrates the process of removing a dependency (rsc.io/sampler@none) using 'go get'. It shows the expected output for downgrades and removals, and how to compare the resulting go.mod file. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_changes.txt#_snippet_1 LANGUAGE: bash CODE: ``` go get rsc.io/sampler@none stderr '^go: downgraded rsc.io/quote v1.5.2 => v1.3.0$' stderr '^go: removed rsc.io/sampler v1.3.0$' cmp go.mod go.mod.downgrade ``` -------------------------------- TITLE: Git Initialization and Committing DESCRIPTION: Sets up Git author information, initializes a Git repository, adds files, creates commits, renames the main branch, and tags a release. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/issue47650.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GIT_AUTHOR_NAME='Bryan C. Mills' env GIT_AUTHOR_EMAIL='bcmills@google.com' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL git init at 2021-08-11T13:52:00-04:00 git add cmd git commit -m 'add cmd/issue47650' git branch -m main git tag v0.1.0 git add go.mod git commit -m 'add go.mod' ``` -------------------------------- TITLE: Internal Package (`v.go`) DESCRIPTION: This Go file defines the `v` package. It is intended to be imported by other packages within the project, demonstrating package structure and dependencies. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/list_symlink_internal.txt#_snippet_2 LANGUAGE: go CODE: ``` package v ``` -------------------------------- TITLE: Running 'go get' Outside a Module DESCRIPTION: Demonstrates the error message when 'go get' is executed outside of a Go module. This behavior is expected and indicates that 'go get' is not supported in this context. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_deprecate_install.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on ! go get example.com/cmd/a stderr '^go: go.mod file not found in current directory or any parent directory.$' stderr '^\t'go get' is no longer supported outside a module.$' ``` -------------------------------- TITLE: Go Install Command Example DESCRIPTION: Demonstrates the `go install -n` command, which prints the commands that would be executed to install the packages but does not actually install them. This is useful for debugging build processes. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_install_submodule.txt#_snippet_5 LANGUAGE: go CODE: ``` cd m/sub go install -n ``` -------------------------------- TITLE: Project Information (.info) DESCRIPTION: Provides metadata about the project version. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/mod/example.net_pkgadded_v1.1.0.txt#_snippet_2 LANGUAGE: text CODE: ``` {"Version":"v1.1.0"} ``` -------------------------------- TITLE: Go Get Empty Patterns DESCRIPTION: Demonstrates the behavior of 'go get' when used with empty or non-matching local module patterns. It shows that a warning is issued only once for such cases. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_nopkgs.txt#_snippet_0 LANGUAGE: shell CODE: ``` cd subdir # 'go get' on empty patterns that are necessarily local to the module # should warn that the patterns are empty, exactly once. go get ./... stderr -count=1 'matched no packages' go get ./... stderr -count=1 'matched no packages' ``` -------------------------------- TITLE: Git Initialization and Initial Commit DESCRIPTION: Initializes a Git repository, sets author information, and makes the first commit with LICENSE and README.md. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/hg/vgotest1.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GIT_AUTHOR_NAME='Russ Cox' env GIT_AUTHOR_EMAIL='rsc@golang.org' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL git init # 0 at 2018-02-19T17:21:09-05:00 git add LICENSE README.md git commit -m 'initial commit' git branch -m master ``` -------------------------------- TITLE: Basic Go Test File DESCRIPTION: A simple Go test file (`main_test.go`) that includes a basic test function `TestF` using the `testing` package. This serves as a minimal example for Go testing. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/test_go111module_cache.txt#_snippet_1 LANGUAGE: go CODE: ``` package main import "testing" func TestF(t *testing.T) {} ``` -------------------------------- TITLE: Install Package After Build DESCRIPTION: Installs a Go package after it has been built. The compiler should not run again if the package is already built. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_commit.txt#_snippet_4 LANGUAGE: shell CODE: ``` go install -x golang.org/x/text/language ! stderr 'compile|cp|gccgo .*language\.a$' ``` -------------------------------- TITLE: Run Basic Go Program DESCRIPTION: Demonstrates the standard 'hello, world' program in Go. This is the entry point for most Go applications. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/go/doc/comment/testdata/code6.txt#_snippet_0 LANGUAGE: go CODE: ``` func main() { fmt.Println("hello, world") } ``` -------------------------------- TITLE: Verify Go Command Installation Directory DESCRIPTION: Checks if the 'go' command is installed in $GOROOT/bin. This test is crucial for ensuring proper Go toolchain setup and addresses a known issue where commands might be incorrectly installed in $GOBIN. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/install_cmd_gobin.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GOBIN=gobin mkdir gobin go list -f '{{.Target}}' cmd/go stdout $GOROOT${/}bin${/}go$GOEXE ``` -------------------------------- TITLE: Go Project Setup and Verification DESCRIPTION: This snippet demonstrates setting up the Go environment for a project, including disabling Go modules, configuring the build cache, and verifying the standard library compilation with specific compiler flags to address issue #29346. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/build_runtime_gcflags.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=off [short] skip # rebuilds all of std # Set up fresh GOCACHE. env GOCACHE=$WORK/gocache mkdir $GOCACHE # Verify the standard library (specifically internal/runtime/atomic) can be # built with -gcflags when -n is given. See golang.org/issue/29346. go build -n -gcflags=all='-l' std stderr 'compile.* internal/runtime/atomic .* -l' ``` -------------------------------- TITLE: Module 'example.net/a' Configuration and Source DESCRIPTION: Defines the go.mod and source file for the 'example.net/a' module. It specifies its own Go version, requires 'example.net/b', and includes a source file that imports 'example.net/b'. This setup is used in conjunction with the main module's 'replace' directives. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_patchbound.txt#_snippet_3 LANGUAGE: go CODE: ``` -- a/go.mod -- module example.net/a go 1.16 require example.net/b v0.1.0 -- a/a.go -- package a import _ "example.net/b" ``` -------------------------------- TITLE: Upgrading Go Module Patterns with 'go get' DESCRIPTION: Shows how to upgrade a package pattern within the main module without affecting the main module's version. This example uses 'go get' with a version suffix and verifies the change in 'go.mod'. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_main.txt#_snippet_2 LANGUAGE: bash CODE: ``` # Upgrading a package pattern not contained in the main module should not # attempt to upgrade the main module. go get rsc.io/quote/...@v1.5.1 grep 'rsc.io/quote v1.5.1' go.mod ``` -------------------------------- TITLE: Go Get and List Module Examples DESCRIPTION: Illustrates how to use `go get` to fetch specific versions of modules and `go list -m all` to view the current module graph. It also shows how to filter output using `stdout` and negation with `! stdout`. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_ambiguous_arg.txt#_snippet_1 LANGUAGE: shell CODE: ``` # Get latest version of module m/p go get m/p # @latest go list -m all stdout '^m/p v0.3.0 ' ! stdout '^m ' # Restore go.mod cp go.mod.orig go.mod # Get specific version v0.1.0 go get m/p@v0.1.0 go list -m all stdout '^m/p v0.1.0 ' ! stdout '^m ' # Example where 'm/p' refers to package m/p in module m # (if no such package exists at the requested version in module m/p) go get m/p@v0.2.0 go list -m all stdout '^m v0.2.0 ' stdout '^m/p v0.1.0 ' # unchanged from the previous case # Repeating the above with module m/p already in the module graph does not # change its meaning. go get m/p@v0.2.0 go list -m all stdout '^m v0.2.0 ' stdout '^m/p v0.1.0 ' ``` -------------------------------- TITLE: Go Module Download and Get DESCRIPTION: Commands to download specific versions of Go modules and get them into the workspace. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/version_replace.txt#_snippet_0 LANGUAGE: go CODE: ``` go mod download example.com/printversion@v0.1.0 example.com/printversion@v1.0.0 ``` LANGUAGE: go CODE: ``` go get example.com/printversion@v0.1.0 ``` -------------------------------- TITLE: Git Initialization and Configuration DESCRIPTION: Initializes a Git repository, sets core configuration options for case sensitivity and Unicode normalization, and configures author information for commits. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/svn/test2-svn-git.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GIT_AUTHOR_NAME='Russ Cox' env GIT_AUTHOR_EMAIL='rsc@golang.org' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL git init git config --add core.ignorecase true git config --add core.precomposeunicode true ``` -------------------------------- TITLE: Go Get Command Example DESCRIPTION: Demonstrates the 'go get' command used to fetch a specific version of a module. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_issue37438.txt#_snippet_0 LANGUAGE: go CODE: ``` go get example.net/a/p@v0.2.0 ``` -------------------------------- TITLE: Go Module Initialization Example DESCRIPTION: Demonstrates the process of initializing a Go module using `go mod init` and listing modules. It shows the expected output and error handling. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_convert_git.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on cd $WORK/test/x ! go list . stderr 'found .git/config in .*[/\]test' stderr '\s*cd \.\. && go mod init' cd .. go mod init go list -m all stdout '^m$' ``` -------------------------------- TITLE: Go Get in Subdirectory Module DESCRIPTION: Shows the outcome of running 'go get' within a subdirectory of a module when the pattern matches no packages. This scenario also results in a 'matched no packages' warning. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_nopkgs.txt#_snippet_3 LANGUAGE: shell CODE: ``` cd ../subdirmod go get work stderr -count=1 'matched no packages' ``` -------------------------------- TITLE: Go Module Initialization and Vendor DESCRIPTION: Sets up the Go environment for module usage and vendors dependencies. This is a common starting point for managing project dependencies. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_getmode_vendor.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on go get rsc.io/quote@v1.5.1 go mod vendor ``` -------------------------------- TITLE: V2 Import with Downloaded Module DESCRIPTION: This example demonstrates fetching and listing dependencies for a module that imports a v2 package. It simulates the process of copying a file, getting the module, and then listing its dependencies, expecting a specific v2 module path. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_multirepo.txt#_snippet_1 LANGUAGE: shell CODE: ``` # v2 import should use a downloaded module # both without an explicit go.mod entry ... cp tmp/use_v2.go x.go go get . go list -deps -f {{.Dir}} stdout 'pkg[\/]mod[\/]rsc.io[\/]quote[\/]v2@v2.0.1$' ``` -------------------------------- TITLE: Install goimports Tool DESCRIPTION: Installs the goimports tool, which automatically formats Go source files and adds missing imports. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_fallback.txt#_snippet_1 LANGUAGE: bash CODE: ``` go get -x -v golang.org/x/tools/cmd/goimports ``` -------------------------------- TITLE: Package Definition DESCRIPTION: A simple Go package definition. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/test_race_install.txt#_snippet_2 LANGUAGE: go CODE: ``` package p ``` -------------------------------- TITLE: Source File: example.com/q/q.go (v1.1.0) DESCRIPTION: This Go source file for `example.com/q` version 1.1.0 imports `example.com/w` and `example.com/z` and prints 'version 1.1.0'. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/work_vendor_prune_all.txt#_snippet_15 LANGUAGE: go CODE: ``` package q import _ "example.com/w" import _ "example.com/z" import "fmt" func PrintVersion() { fmt.Println("version 1.1.0") } ``` -------------------------------- TITLE: Go `net/http` Client Example DESCRIPTION: Shows how to make HTTP requests using the `net/http` client in Go. This includes GET, POST, and handling responses. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/compress/testdata/e.txt#_snippet_29 LANGUAGE: go CODE: ``` package main import ( "fmt" "io/ioutil" "log" "net/http" "strings" ) func main() { // Example GET request resp, err := http.Get("https://httpbin.org/get") if err != nil { log.Fatalf("GET request failed: %v", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalf("Failed to read response body: %v", err) } fmt.Printf("GET Response Status: %s\n", resp.Status) fmt.Printf("GET Response Body:\n%s\n", string(body)) // Example POST request postData := "{\"name\": \"GoClient\", \"version\": \"1.0\"}" resp, err = http.Post("https://httpbin.org/post", "application/json", strings.NewReader(postData)) if err != nil { log.Fatalf("POST request failed: %v", err) } defer resp.Body.Close() body, err = ioutil.ReadAll(resp.Body) if err != nil { log.Fatalf("Failed to read response body: %v", err) } fmt.Printf("POST Response Status: %s\n", resp.Status) fmt.Printf("POST Response Body:\n%s\n", string(body)) } ``` -------------------------------- TITLE: Go Get Nested and Non-existent Modules DESCRIPTION: Illustrates how 'go get' handles patterns that could potentially match nested modules or modules that do not exist. It shows the expected module resolution error for a 404 Not Found scenario. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_nopkgs.txt#_snippet_1 LANGUAGE: shell CODE: ``` # 'go get' on patterns that could conceivably match nested modules # should report a module resolution error. go get example.net/emptysubdir/... # control case ! go get example.net/emptysubdir/subdir/... ! stderr 'matched no packages' stderr '^go: example\.net/emptysubdir/subdir/\.\.\.: module example\.net/emptysubdir/subdir: reading http://.*: 404 Not Found\n\tserver response: 404 page not found\n\z' ``` -------------------------------- TITLE: SVN Repository Setup and Initialization DESCRIPTION: Initializes the SVN repository structure and sets up necessary directories and permissions. It also configures the environment for SVN operations. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/svn/hello.txt#_snippet_0 LANGUAGE: shell CODE: ``` mkdir db/transactions mkdir db/txn-protorevs chmod 0755 hooks/pre-revprop-change env ROOT=$PWD ``` -------------------------------- TITLE: Git Initialization and Branching DESCRIPTION: Sets up Git author information, initializes a repository, adds files, commits changes, creates a new branch, and tags a release. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/vcstest/git/tagtests.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GIT_AUTHOR_NAME='Jay Conrod' env GIT_AUTHOR_EMAIL='jayconrod@google.com' env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME env GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL git init git add go.mod tagtests.go git commit -m 'create module tagtests' git branch -m master git branch b git add v0.2.1 git commit -m 'v0.2.1' git tag 'v0.2.1' git checkout b git add 'v0.2.2' git commit -m 'v0.2.2' git tag 'v0.2.2' git checkout master git merge b -m 'merge' ``` -------------------------------- TITLE: Running 'go get' Inside a Module (Executable) DESCRIPTION: Illustrates 'go get' behavior for an executable main package within a module. It mentions that previous Go versions (1.16, 1.17) suggested '-d', but this is being discouraged. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/mod_get_deprecate_install.txt#_snippet_2 LANGUAGE: bash CODE: ``` cp go.mod.orig go.mod # 'go get' inside a module with an executable does not print a message. # In 1.16 and 1.17, 'go get' did print a message in this case suggesting the # use of -d. In 1.18, -d is a no-op, and we'd like to begin discouraging # its use. go get example.com/cmd/a ! stderr deprecated ! stderr 'no longer installs' cp go.mod.orig go.mod ``` -------------------------------- TITLE: Go Project Module Definition DESCRIPTION: A standard 'go.mod' file defining the module path and Go version for a project. This is essential for managing dependencies in Go. SOURCE: https://github.com/golang/go/blob/go1.25.0/src/cmd/go/testdata/script/install_relative_gobin_fail.txt#_snippet_1 LANGUAGE: go CODE: ``` module triv go 1.16 ```