================ CODE SNIPPETS ================ TITLE: Example Go Program DESCRIPTION: A minimal Go program that serves as an example for building and installation. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/install_cross_gobin.txt#_snippet_3 LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Basic Cross-Compilation Setup DESCRIPTION: Sets up environment variables for cross-compilation to a 386 architecture and then to amd64, followed by installing a command. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 ``` -------------------------------- TITLE: Building and Installing Go Packages DESCRIPTION: Demonstrates building and installing Go packages, specifically 'm/isarchive' and 'cmd/gofmt'. It shows how to install them into the GOBIN directory and execute them. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_5 LANGUAGE: shell CODE: ``` go install m/isarchive & go build cmd/gofmt exists -exec gofmt$GOEXE rm gofmt$GOEXE ! exists gofmt$NONEXE ``` -------------------------------- TITLE: Go Project Module Definition DESCRIPTION: An example `go.mod` file defining the module name and Go version for a project. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/install_relative_gobin_fail.txt#_snippet_1 LANGUAGE: go CODE: ``` module triv go 1.16 ``` -------------------------------- TITLE: Install Module (Example) DESCRIPTION: Demonstrates installing a Go module. The example shows a command that might fail if the module is not found or accessible, indicated by `stderr` output. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_install_hint.txt#_snippet_2 LANGUAGE: go CODE: ``` ! go install github.com/notrequired ``` -------------------------------- TITLE: Go Main Package Example DESCRIPTION: These are simple Go programs defining the 'main' package with an empty 'main' function, typically used as placeholders or starting points for Go applications. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_init_path.txt#_snippet_2 LANGUAGE: go CODE: ``` -- x/main.go -- package main func main() {} ``` LANGUAGE: go CODE: ``` -- y/main.go -- package main func main() {} ``` -------------------------------- TITLE: Go Project Setup and Package Installation DESCRIPTION: This snippet shows how to create a temporary directory for Go packages and install the standard library with the race detector enabled. It utilizes shell commands for directory creation and Go's install command. SOURCE: https://github.com/golang/go/blob/go1_23_10/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: Go Project Setup and Execution DESCRIPTION: This snippet demonstrates setting up a Go project environment using symlinks and the GOPATH variable. It then proceeds to build and run Go programs, showcasing dependency handling with a vendor directory. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 ``` LANGUAGE: go CODE: ``` package main import _ `v` func main () {} ``` LANGUAGE: go CODE: ``` package v ``` -------------------------------- TITLE: Basic Go Program DESCRIPTION: A minimal Go program with a `main` package and an empty `main` function. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/install_relative_gobin_fail.txt#_snippet_2 LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Go Module Initialization and Dependency Management DESCRIPTION: Demonstrates setting the GO111MODULE environment variable to 'on' for module support. It also shows examples of fetching and installing Go packages, including a case with an invalid path. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 . ``` -------------------------------- TITLE: Windows-Specific Build and Execution DESCRIPTION: Shows how to build a Go program on Windows, placing the executable in a 'bin' directory with the correct extension, and then executing it. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_3 LANGUAGE: shell CODE: ``` [GOOS:windows] ! exists bin [GOOS:windows] go build -o bin\x x.go [GOOS:windows] exists -exec bin\x [GOOS:windows] rm bin ``` -------------------------------- TITLE: Go Module Vendor Setup and Listing DESCRIPTION: This snippet demonstrates setting up the Go environment for vendoring, fetching a specific module version, creating the vendor directory, and then listing modules from the vendor directory. It includes examples of successful listing and expected output patterns. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 env GOPATH=$WORK/empty env GOPROXY=file:///nonexist go list -mod=vendor go list -mod=vendor -f '{{with .Module}}{{.Path}} {{.Version}}{{end}} {{.Dir}}' all stdout '^rsc.io/quote v1.5.1 .*vendor[\/]rsc.io[\/]quote$' stdout '^golang.org/x/text v0.0.0.* .*vendor[\/]golang.org[\/]x[\/]text[\/]language$' ``` -------------------------------- TITLE: Populate go.sum and Get Dependencies DESCRIPTION: This snippet shows how to populate the go.sum file and fetch dependencies using 'go get'. It's a common first step when initializing a Go project or updating its dependencies. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_list_dir.txt#_snippet_0 LANGUAGE: shell CODE: ``` go get ``` -------------------------------- TITLE: Install echo command DESCRIPTION: Installs the 'echo' command for use in environments where it might not be pre-installed, such as Windows. It sets the GOBIN environment variable and then installs the echo.go program. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/generate.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GOBIN=$WORK/tmp/bin go install echo.go env PATH=$GOBIN:${PATH} ``` -------------------------------- TITLE: Building into a Specific Directory DESCRIPTION: Demonstrates building a Go program into a 'bin' directory with a specific executable name. It includes checks for the directory's existence and cleans up afterwards. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_2 LANGUAGE: shell CODE: ``` ! exists bin go build -o bin/x x.go exists -exec bin/x rm bin ``` -------------------------------- TITLE: Building Libraries and Verifying with External Programs DESCRIPTION: Builds the 'sync/atomic' package as a library ('myatomic.a') and then uses the 'isarchive' program to verify it. It also checks for the non-existence of executables. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_8 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 ``` -------------------------------- TITLE: Go Module Initialization and Dependency Fetching DESCRIPTION: This snippet demonstrates initializing a Go module, fetching an unstable dependency, and listing the modules. It includes commands for copying a module file and getting a package. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 cp x.go.txt x.go cp go.mod.empty go.mod go list ``` -------------------------------- TITLE: Basic Go Build and Execution DESCRIPTION: Compiles a Go program 'x.go', creates an executable, and then checks for its existence and executes it. It also handles cleanup by removing the executable. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 ``` -------------------------------- TITLE: Go Module Initialization and Basic Get DESCRIPTION: Sets up the Go module environment and performs a basic 'go get' operation. It also includes checks for expected output in the go.mod file. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_main.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on cp go.mod.orig go.mod # relative and absolute paths must be within the main module. ! go get .. stderr '^go: \.\. \($WORK'[/\]gopath) is not within module rooted at '$WORK'[/\]gopath[/\]src$' ! go get $WORK stderr '^go: '$WORK' is not within module rooted at '$WORK'[/\]gopath[/\]src$' ! go get ../... stderr '^go: \.\./\.\.\. \($WORK'[/\]gopath([/\]...)?) is not within module rooted at '$WORK'[/\]gopath[/\]src$' ! go get $WORK/... stderr '^go: '$WORK'[/\]\.\.\. is not within module rooted at '$WORK'[/\]gopath[/\]src$' ``` LANGUAGE: shell CODE: ``` # @patch and @latest within the main module refer to the current version. # The main module won't be upgraded, but missing dependencies will be added. go get rsc.io/x grep 'rsc.io/quote v1.5.2' go.mod go get rsc.io/x@upgrade grep 'rsc.io/quote v1.5.2' go.mod cp go.mod.orig go.mod go get rsc.io/x@patch grep 'rsc.io/quote v1.5.2' go.mod cp go.mod.orig go.mod ``` LANGUAGE: shell 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 ``` LANGUAGE: shell CODE: ``` # The main module cannot be updated to a specific version. ! go get rsc.io@v0.1.0 stderr '^go: can''t request version "v0.1.0" of the main module (rsc.io)$' ``` LANGUAGE: shell CODE: ``` # A package in the main module can't be upgraded either. ! go get rsc.io/x@v0.1.0 stderr '^go: package rsc.io/x is in the main module, so can''t request version v0.1.0$' ``` LANGUAGE: shell CODE: ``` # Nor can a pattern matching packages in the main module. ! go get rsc.io/x/...@latest stderr '^go: pattern rsc.io/x/... matches package rsc.io/x in the main module, so can''t request version latest$' ``` -------------------------------- TITLE: Go Module File Structure DESCRIPTION: This section provides the content of the `go.mod` and `x.go` files used in the preceding examples, illustrating a basic Go module setup. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_vendor_build.txt#_snippet_4 LANGUAGE: go CODE: ``` -- go.mod -- module m go 1.12 -- x.go -- package x import _ "rsc.io/quote" ``` -------------------------------- TITLE: Go Modules Vendoring Example DESCRIPTION: This snippet shows the environment setup and command to list modules in a vendored Go project. It also includes expected stdout and a sample go.mod file. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_patterns_vendor.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on go list -mod=vendor example.com/... stdout ^example.com/x$ stdout ^example.com/x/y$ ! stdout ^example.com/x/vendor ``` LANGUAGE: go CODE: ``` -- go.mod -- module example.com/m ``` -------------------------------- TITLE: Go Environment Variables and goimports Installation DESCRIPTION: Configures Go environment variables for module support and proxy settings, then installs the goimports tool. This setup ensures proper dependency management and code formatting. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_fallback.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on [!net:golang.org] skip [!net:proxy.golang.org] skip env GOPROXY=https://proxy.golang.org,direct env GOSUMDB=off go get -x -v golang.org/x/tools/cmd/goimports stderr '# get https://proxy.golang.org/golang.org/x/tools/@v/list' ! stderr '# get https://golang.org' ``` -------------------------------- TITLE: Go Module Versioning Example DESCRIPTION: Illustrates how Go handles different versions of the same module. It shows fetching specific versions of a module (`example.com/stack`) and running a program that depends on it, verifying that the correct module version is used. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_cache_trimpath.txt#_snippet_1 LANGUAGE: bash CODE: ``` # Two distinct versions of the same module with identical content should # still be cached separately. # Verifies golang.org/issue/35412. go get example.com/stack@v1.0.0 go run -trimpath printstack.go stdout '^example.com/stack@v1.0.0/stack.go$' go get example.com/stack@v1.0.1 go run -trimpath printstack.go stdout '^example.com/stack@v1.0.1/stack.go$' ``` -------------------------------- TITLE: Building a Go Program DESCRIPTION: Compiles a Go program named 'hello.go'. The example also shows a potential error message related to missing libraries during the build process. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_arm.txt#_snippet_1 LANGUAGE: shell CODE: ``` go build hello.go ! stderr 'unable to find math.a' ``` LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Go Get and Toolchain Update Example DESCRIPTION: This snippet demonstrates a scenario where `go get` is used to install packages that trigger a Go toolchain update. It shows the initial setup, the `go get` command, and the expected output in stderr, followed by verification steps using `grep` to confirm changes in `go.mod` and `go.work` files. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/work_get_toolchain.txt#_snippet_0 LANGUAGE: shell CODE: ``` env TESTGO_VERSION=go1.21 env TESTGO_VERSION_SWITCH=switch env GOTOOLCHAIN=auto cp go.mod.new go.mod cp go.work.new go.work go get rsc.io/needgo121 rsc.io/needgo122 rsc.io/needgo123 rsc.io/needall stderr '^go: rsc.io/needall@v0.0.1 requires go >= 1.23; switching to go1.23.9$' stderr '^go: added rsc.io/needall v0.0.1' grep 'go 1.23$' go.mod grep 'go 1.23$' go.work grep 'toolchain go1.23.9' go.mod grep 'toolchain go1.23.9' go.work ``` -------------------------------- TITLE: Cross-Compilation with Explicit GOBIN DESCRIPTION: Demonstrates cross-compilation and installation using an explicit GOBIN path, highlighting that it does not create subdirectories for the target architecture. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/install_cross_gobin.txt#_snippet_1 LANGUAGE: bash CODE: ``` # cross-compile install with explicit GOBIN cannot make subdirectory env GOBIN=$WORK/bin ! go install mycmd ! exists $GOBIN/linux_$GOARCH ``` -------------------------------- TITLE: Basic Main Package DESCRIPTION: A minimal Go program defining the 'main' package with an empty 'main' function. This serves as a basic executable. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_11 LANGUAGE: go CODE: ``` package main func main() {} ``` -------------------------------- TITLE: Go Project Setup and Listing DESCRIPTION: This snippet demonstrates setting up a Go project environment by disabling module support, creating a workspace, creating a directory, symlinking it, copying a Go file into the new directory, setting the GOPATH, and then using `go list` to display the project root. It highlights essential Go development environment configurations. SOURCE: https://github.com/golang/go/blob/go1_23_10/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: Go Module File Example DESCRIPTION: An example of a go.mod file, which defines the module path and the Go version required for the project. This file is crucial for Go's module system. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/import_ignore.txt#_snippet_1 LANGUAGE: go CODE: ``` module m.test go 1.16 ``` -------------------------------- TITLE: Architecture-Specific Assembly Instructions (ARM Example) DESCRIPTION: The Go toolchain defines assembly instructions for various architectures. These are listed as constants in the `obj` support library for each architecture, typically starting with 'A'. For example, in `src/cmd/internal/obj/arm/a.out.go`. SOURCE: https://github.com/golang/go/blob/go1_23_10/doc/asm.html#_snippet_16 LANGUAGE: assembly CODE: ``` // Example constants from src/cmd/internal/obj/arm/a.out.go const ( AAND = obj.ABaseARM + obj.A_ARCHSPECIFIC + iota AEOR ASUB ARSB AADD ) // In assembly source, these are written without the 'A' prefix: // AND // EOR // SUB // RSB // ADD ``` -------------------------------- TITLE: Go Module Setup and Build DESCRIPTION: This snippet demonstrates setting up Go modules, fetching a dependency, and building the project. It includes environment configuration for module mode and the go build command. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_case_cgo.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GO111MODULE=on go get rsc.io/CGO go build rsc.io/CGO ``` LANGUAGE: go CODE: ``` -- go.mod -- module x ``` -------------------------------- TITLE: Building and Executing with a Go Program DESCRIPTION: Compiles a Go program 'p.go' and then uses a separate Go program 'isarchive' to verify if the compiled output 'p.a' is a valid archive. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_4 LANGUAGE: shell CODE: ``` go build p.go ! exists p ! exists p.a ! exists p.o ! exists p.exe wait # for isarchive go build -o p.a p.go exists p.a exec $GOBIN/isarchive p.a ``` -------------------------------- TITLE: List Go Module Information DESCRIPTION: Executes the 'go list -m' command to display the current Go module path. The output is expected to match 'example.com'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_init_empty.txt#_snippet_2 LANGUAGE: shell CODE: ``` go list -m stdout '^example.com$' ``` -------------------------------- TITLE: Go Get All with Test Dependencies DESCRIPTION: This example illustrates that 'go get all' considers test dependencies, similar to 'go get -t'. It fetches all dependencies and verifies that 'rsc.io/quote' is included in the go.mod file. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_test.txt#_snippet_5 LANGUAGE: shell CODE: ``` # 'go get all' should consider test dependencies with or without -t. cp go.mod.empty go.mod go get all grep 'rsc.io/quote v1.5.2$' go.mod ``` -------------------------------- TITLE: Go Get Command DESCRIPTION: Fetches packages and their dependencies. The -v flag provides verbose output, showing the download and build process. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_list_direct.txt#_snippet_3 LANGUAGE: bash CODE: ``` go get -v ``` -------------------------------- TITLE: Git Initialization and Configuration DESCRIPTION: Sets up Git author information using environment variables and initializes a new Git repository. This is a common starting point for version control in a project. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/vcstest/git/v2sub.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_AUTHOR_EMAIL git init ``` -------------------------------- TITLE: Diff Output Example DESCRIPTION: This is a standard diff output format showing the changes between two files. Lines starting with '-' are from the old file, and lines starting with '+' are from the new file. Lines starting with ' ' are unchanged. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/internal/diff/testdata/eof1.txt#_snippet_1 LANGUAGE: diff CODE: ``` diff old new --- old +++ new @@ -1,3 +1,3 @@ a b -c +c \ No newline at end of file ``` -------------------------------- TITLE: List Go Packages DESCRIPTION: Executes the 'go list' command to list packages in the current module. The output is expected to match 'example.com'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_init_empty.txt#_snippet_3 LANGUAGE: shell CODE: ``` go list stdout '^example.com$' ``` -------------------------------- TITLE: Go Project Initialization and Build DESCRIPTION: Demonstrates the commands to initialize a Go module, set the environment for module usage, and build the project. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_string_alias.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on go mod init golang.org/issue/27584 go build . ``` -------------------------------- TITLE: Module Configuration for example.com/a DESCRIPTION: Specifies the module name, Go version, and dependencies for 'example.com/a', including a local replacement for 'example.com/p'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_7 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: Go Project Setup and Execution DESCRIPTION: This snippet details the process of setting up a Go project directory structure, copying source files, creating symlinks for module management, and executing Go commands like `go list`, `go run`, and `go build`. SOURCE: https://github.com/golang/go/blob/go1_23_10/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 Workspace Setup and Module Installation DESCRIPTION: This snippet demonstrates the setup of a Go workspace with multiple modules and the execution of `go install -n` within a submodule directory. It verifies the correct handling of module dependencies and installation commands in a regression test scenario. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/work_install_submodule.txt#_snippet_0 LANGUAGE: shell CODE: ``` cd m/sub go install -n ``` LANGUAGE: go CODE: ``` -- go.work -- go 1.18 use ( ./m ./m/sub ) ``` LANGUAGE: go CODE: ``` -- m/go.mod -- module example.com/m go 1.18 ``` LANGUAGE: go CODE: ``` -- m/m.go -- package m func M() {} ``` LANGUAGE: go CODE: ``` -- m/sub/go.mod -- module example.com/m/sub go 1.18 require example.com/m v1.0.0 ``` LANGUAGE: go CODE: ``` -- m/sub/main.go -- package main import "example.com/m" func main() { m.M() } ``` -------------------------------- TITLE: Git Initialization and Configuration DESCRIPTION: Sets up Git for a project by initializing the repository and configuring author information for commits. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/vcstest/git/prefixtagtests.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_AUTHOR_EMAIL at 2019-05-09T18:35:00-04:00 git init ``` -------------------------------- TITLE: Standard Command Installation Directory DESCRIPTION: Shows the expected installation directory for a cross-compiled standard command, which includes the GOARCH in the path. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/install_cross_gobin.txt#_snippet_2 LANGUAGE: bash CODE: ``` # 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: Handling Version Conflicts with 'go get' DESCRIPTION: This example demonstrates how 'go get' handles conflicting version requirements between modules. It attempts to get 'rsc.io/sampler@v1.0.0' and 'rsc.io/quote@v1.5.2' simultaneously, along with 'golang.org/x/text@none'. The output shows an error message indicating a version mismatch for 'rsc.io/sampler'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_downgrade.txt#_snippet_3 LANGUAGE: shell CODE: ``` ! go get rsc.io/sampler@v1.0.0 rsc.io/quote@v1.5.2 golang.org/x/text@none ! stderr add|remove|upgrad|downgrad stderr '^go: rsc.io/quote@v1.5.2 requires rsc.io/sampler@v1.3.0, not rsc.io/sampler@v1.0.0$' go list -m all stdout 'rsc.io/quote v1.5.1' stdout 'rsc.io/sampler v1.3.0' ``` -------------------------------- TITLE: Go Get with Test Dependencies DESCRIPTION: This example shows how 'go get -t' includes test dependencies. It fetches dependencies for 'm/a' and verifies that test-related packages like 'rsc.io/quote' are added to the go.mod file. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_test.txt#_snippet_1 LANGUAGE: shell CODE: ``` # 'go get -t' should consider test dependencies of the named package. cp go.mod.empty go.mod go get -t m/a grep 'rsc.io/quote v1.5.2$' go.mod ``` -------------------------------- TITLE: Go Project Module Definition DESCRIPTION: Example of a `go.mod` file defining a Go module. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/env_issue46807.txt#_snippet_2 LANGUAGE: go CODE: ``` -- $GOPATH/go.mod -- module bug go 1.21 ``` -------------------------------- TITLE: Go Project Module Definition DESCRIPTION: Provides an example of a 'go.mod' file, which is essential for defining a Go module. This file specifies the module path, enabling dependency management for the project. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_install_versioned.txt#_snippet_1 LANGUAGE: go CODE: ``` -- go.mod -- module m ``` -------------------------------- TITLE: Go Installation with GOBIN DESCRIPTION: Demonstrates setting the GOBIN environment variable and attempting to install a Go package. It highlights the error when GOBIN is not an absolute path. SOURCE: https://github.com/golang/go/blob/go1_23_10/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: Sample Go Program DESCRIPTION: A simple 'hello world' program written in Go that prints a message to the console. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/vcstest/git/hello.txt#_snippet_3 LANGUAGE: go CODE: ``` package main func main() { println("hello, world") } ``` -------------------------------- TITLE: Go Source File Example DESCRIPTION: A simple Go source file defining a function `F` that returns an integer. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/list_dedup_packages.txt#_snippet_2 LANGUAGE: go CODE: ``` package xtestonly func F() int { return 42 } ``` -------------------------------- TITLE: Go Get: Transitive Dependency Resolution Failure DESCRIPTION: Shows a scenario where 'go get' fails due to a version constraint that prevents the resolution of a transitive dependency. The example includes the go.mod and m.go files to set up the context and the expected error message. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_downgrade_missing.txt#_snippet_2 LANGUAGE: bash 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: Running a Go Program Directly DESCRIPTION: This snippet shows how to compile and run a Go program directly from a source file using the `go run` command. It assumes the Go toolchain is installed and configured. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/run_hello_pkg.txt#_snippet_0 LANGUAGE: shell CODE: ``` go run m/hello stderr 'hello, world' ``` -------------------------------- TITLE: Install Go terminal/console support DESCRIPTION: Installs the Go terminal and console support packages using the go get command. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/vendor/golang.org/x/term/README.md#_snippet_0 LANGUAGE: go CODE: ``` go get -u golang.org/x/term ``` -------------------------------- TITLE: Go Module and Proxy Configuration DESCRIPTION: Sets up Go modules and configures the GOPROXY to 'direct' for fetching dependencies. This is a common starting point for Go development environments. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/govcs.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on env proxy=$GOPROXY env GOPROXY=direct ``` -------------------------------- TITLE: Patching Dependencies with Go Get DESCRIPTION: Updates a specific dependency to a new version using 'go get -u=patch'. The patch flag attempts to update dependencies as far as possible without conflicting with other command-line arguments. This example shows updating 'example.net/a' to v0.2.0. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_patchbound.txt#_snippet_1 LANGUAGE: go CODE: ``` go get -u=patch example.net/a@v0.2.0 go list -m all stdout '^example.net/a v0.2.0 ' stdout '^example.net/b v0.1.1 ' ``` -------------------------------- TITLE: Module Configuration for example.com/q DESCRIPTION: Defines the module name and Go version for 'example.com/q'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_10 LANGUAGE: go CODE: ``` module example.com/q go 1.21 ``` -------------------------------- TITLE: Install fstestutil with specific environment variables DESCRIPTION: This command uses 'go get' to install the 'fstestutil' package from 'bazil.org/fuse/fs'. It specifies environment variables GONOSUMDB, GO111MODULE, and GOPROXY to control dependency resolution and module behavior. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/get_404_meta.txt#_snippet_0 LANGUAGE: bash CODE: ``` env GONOSUMDB=bazil.org,github.com,golang.org env GO111MODULE=on env GOPROXY=direct go get bazil.org/fuse/fs/fstestutil ``` -------------------------------- TITLE: Simple Package Definition DESCRIPTION: Defines a Go package named 'p'. This is typically used for libraries or modules that are not directly executable. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_12 LANGUAGE: go CODE: ``` package p ``` -------------------------------- TITLE: Go Module Patch Update Examples DESCRIPTION: Demonstrates various scenarios of updating Go modules using 'go get' with patch updates. It covers initial setup, applying patch updates to all dependencies, specific modules, and handling cases where modules are not yet in the build list or are standard library packages. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_upgrade_patch.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on # Initially, we are at v1.0.0 for all dependencies. go get cp go.mod go.mod.orig go list -m all stdout '^patch.example.com/direct v1.0.0' stdout '^patch.example.com/indirect v1.0.0' ! stdout '^patch.example.com/depofdirectpatch' # @patch should be rejected for modules not already in the build list. ! go get patch.example.com/depofdirectpatch@patch stderr '^go: can\'t query version \"patch\" of module patch.example.com/depofdirectpatch: no existing version is required $' cmp go.mod.orig go.mod # get -u=patch, with no arguments, should patch-update all dependencies # of the package in the current directory, pulling in transitive dependencies # and also patching those. cp go.mod.orig go.mod go get -u=patch go list -m all stdout '^patch.example.com/direct v1.0.1' stdout '^patch.example.com/indirect v1.0.1' stdout '^patch.example.com/depofdirectpatch v1.0.0' # 'get all@patch' should patch the modules that provide packages in 'all'. cp go.mod.orig go.mod go get all@patch go list -m all stdout '^patch.example.com/direct v1.0.1' stdout '^patch.example.com/indirect v1.0.1' stdout '^patch.example.com/depofdirectpatch v1.0.0' # ...but 'all@patch' should fail if any of the affected modules do not already # have a selected version. cp go.mod.orig go.mod go mod edit -droprequire=patch.example.com/direct cp go.mod go.mod.dropped ! go get all@patch stderr '^go: all@patch: can\'t query version \"patch\" of module patch.example.com/direct: no existing version is required $' cmp go.mod.dropped go.mod # Requesting the direct dependency with -u=patch but without an explicit version # should patch-update it and its dependencies. cp go.mod.orig go.mod go get -u=patch patch.example.com/direct go list -m all stdout '^patch.example.com/direct v1.0.1' stdout '^patch.example.com/indirect v1.0.1' stdout '^patch.example.com/depofdirectpatch v1.0.0' # Requesting only the indirect dependency should not update the direct one. cp go.mod.orig go.mod go get -u=patch patch.example.com/indirect go list -m all stdout '^patch.example.com/direct v1.0.0' stdout '^patch.example.com/indirect v1.0.1' ! stdout '^patch.example.com/depofdirectpatch' # @patch should apply only to the specific module, # but the result must reflect its upgraded requirements. cp go.mod.orig go.mod go get patch.example.com/direct@patch go list -m all stdout '^patch.example.com/direct v1.0.1' stdout '^patch.example.com/indirect v1.0.0' stdout '^patch.example.com/depofdirectpatch v1.0.0' # An explicit @patch should override a general -u. cp go.mod.orig go.mod go get -u patch.example.com/direct@patch go list -m all stdout '^patch.example.com/direct v1.0.1' stdout '^patch.example.com/indirect v1.1.0' stdout '^patch.example.com/depofdirectpatch v1.0.0' # An explicit @latest should override a general -u=patch. cp go.mod.orig go.mod go get -u=patch patch.example.com/direct@latest go list -m all stdout '^patch.example.com/direct v1.1.0' stdout '^patch.example.com/indirect v1.0.1' ! stdout '^patch.example.com/depofdirectpatch' # Standard library packages cannot be upgraded explicitly. cp go.mod.orig go.mod ! go get cmd/vet@patch stderr 'go: can\'t request explicit version \"patch\" of standard library package cmd/vet $' # However, standard-library packages without explicit versions are fine. go get -u=patch cmd/go # We can upgrade to a new version of a module with no root package. go get example.com/noroot@v1.0.0 go list -m all stdout '^example.com/noroot v1.0.0$' go get example.com/noroot@patch go list -m all stdout '^example.com/noroot v1.0.1$' ``` -------------------------------- TITLE: Go Get -t -u for Updating Test Dependencies DESCRIPTION: This example demonstrates updating test dependencies using 'go get -t -u'. It first requires a specific version of 'rsc.io/quote', then updates dependencies for 'm/a', and verifies that the test dependency is updated to the expected version. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_test.txt#_snippet_3 LANGUAGE: shell CODE: ``` # 'go get -t -u' should update test dependencies of the named package. cp go.mod.empty go.mod go mod edit -require=rsc.io/quote@v1.5.1 go get -t -u m/a grep 'rsc.io/quote v1.5.2$' go.mod ``` -------------------------------- TITLE: Wildcard Package Installation DESCRIPTION: Demonstrates installing packages using wildcards. It shows how 'go install' handles matching main packages and non-main packages, including error scenarios and warnings. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_install_pkg_version.txt#_snippet_11 LANGUAGE: shell CODE: ``` # Installs all main packages within a module path go install example.com/cmd/...@v1.0.0 # Installs packages, expecting a warning for non-matching wildcards go install example.com/cmd/nomatch...@v1.0.0 # Installs packages, expecting a warning for matching only non-main packages go install example.com/cmd/err...@v1.0.0 ``` -------------------------------- TITLE: Install and Execute mtime and sametime Utilities DESCRIPTION: This snippet demonstrates setting the GOBIN environment variable, installing the 'mtime' and 'sametime' Go programs, and then executing 'mtime' to get a file's modification time and 'sametime' to compare two timestamps. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/test_race_install_cgo.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GOBIN=$WORK/bin go install m/mtime m/sametime go tool -n cgo cp stdout cgopath.txt exec $GOBIN/mtime cgopath.txt # get the mtime of the file whose name is in cgopath.txt cp stdout cgotime_before.txt # For this test, we don't actually care whether 'go test -race -i' succeeds. # It may fail if GOROOT is read-only (perhaps it was installed as root). # We only care that it does not overwrite cmd/cgo regardless. ? go test -race -i runtime/race exec $GOBIN/mtime cgopath.txt # get the mtime of the file whose name is in cgopath.txt cp stdout cgotime_after.txt exec $GOBIN/sametime cgotime_before.txt cgotime_after.txt ``` -------------------------------- TITLE: Warn About Retracted Version During Get DESCRIPTION: Illustrates the warning message displayed by 'go get' when attempting to retrieve a retracted version of a module. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_retract_rename.txt#_snippet_3 LANGUAGE: shell CODE: ``` go get stderr '^go: warning: example.com/retract/rename@v1.0.0-bad: retracted by module author: bad$' ``` -------------------------------- TITLE: Module Configuration for example.com/b DESCRIPTION: Specifies the module name, Go version, and dependencies for 'example.com/b', including a local replacement for 'example.com/q'. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/work_vendor_modules_txt_consistent.txt#_snippet_8 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: Verify Go Command Installation Path DESCRIPTION: This snippet checks that the 'go' command is installed in the $GOROOT/bin directory, not $GOBIN. It sets GOBIN to a temporary directory and then uses 'go list' to find the target path of the 'cmd/go' package. SOURCE: https://github.com/golang/go/blob/go1_23_10/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: Environment Setup and Git Initialization DESCRIPTION: Sets up author and committer information for Git, creates necessary directories, and initializes a Git repository with specific configurations. SOURCE: https://github.com/golang/go/blob/go1_23_10/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_AUTHOR_EMAIL mkdir db/transactions mkdir db/txn-protorevs chmod 0755 hooks/pre-revprop-change env ROOT=$PWD cd .checkout [GOOS:windows] svn checkout file:///$ROOT . [!GOOS:windows] svn checkout file://$ROOT . git init git config --add core.ignorecase true git config --add core.precomposeunicode true ``` -------------------------------- TITLE: Go Test File Example DESCRIPTION: A Go test file that imports the package under test and verifies the functionality of the `F` function. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/list_dedup_packages.txt#_snippet_3 LANGUAGE: go CODE: ``` package xtestonly_test import ( testing xtestonly ) func TestF(t *testing.T) { if x := xtestonly.F(); x != 42 { t.Errorf("f.F() = %d, want 42", x) } } ``` -------------------------------- TITLE: Go Module Initialization and Version Specification DESCRIPTION: Defines a Go module named 'm' and specifies the Go version to be used (1.16). This is a standard go.mod file content. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_output.txt#_snippet_10 LANGUAGE: go CODE: ``` module m go 1.16 ``` -------------------------------- TITLE: Preventing Downgrades with go get -u DESCRIPTION: Demonstrates that `go get -u` will not downgrade a dependency from a pseudo-version to an earlier tag. It shows the verification step. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_upgrade.txt#_snippet_4 LANGUAGE: shell CODE: ``` # get -u should not jump off newer pseudo-version to earlier tag go get -u grep 'rsc.io/quote v0.0.0-20180628003336-dd9747d19b04' go.mod ``` -------------------------------- TITLE: Downgrading with go get -u DESCRIPTION: Shows that `go get -u` can upgrade a dependency from an earlier pseudo-version to a newer tag. It verifies the change in `go.mod`. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_get_upgrade.txt#_snippet_6 LANGUAGE: shell CODE: ``` # get -u should jump off earlier pseudo-version to newer tag go get -u grep 'rsc.io/quote v1.5.2' go.mod ``` -------------------------------- TITLE: Basic Go Build and Execution DESCRIPTION: Demonstrates a standard Go build process and execution of the compiled binary. Assumes a simple 'Hello, world' program. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/build_static.txt#_snippet_0 LANGUAGE: bash CODE: ``` go build exec ./hello stdout Hello ``` -------------------------------- TITLE: Go Module Installation and Build DESCRIPTION: Demonstrates how to set up Go modules, install a package, and build the executable and test binaries with default naming conventions. SOURCE: https://github.com/golang/go/blob/go1_23_10/src/cmd/go/testdata/script/mod_build_versioned.txt#_snippet_0 LANGUAGE: shell CODE: ``` env GO111MODULE=on go get rsc.io/fortune/v2 # The default executable name shouldn't be v2$GOEXE go build rsc.io/fortune/v2 ! exists v2$GOEXE exists fortune$GOEXE # The default test binary name shouldn't be v2.test$GOEXE go test -c rsc.io/fortune/v2 ! exists v2.test$GOEXE exists fortune.test$GOEXE ``` LANGUAGE: go CODE: ``` -- go.mod -- module scratch ```