class Go < Formula desc "Open source programming language to build simple/reliable/efficient software" homepage "https://go.dev/" url "https://go.dev/dl/go1.25.5.src.tar.gz" mirror "https://fossies.org/linux/misc/go1.25.5.src.tar.gz" sha256 "22a5fd0a91efcd28a1b0537106b9959b2804b61f59c3758b51e8e5429c1a954f" license "BSD-3-Clause" head "https://go.googlesource.com/go.git", branch: "master" livecheck do url "https://go.dev/dl/?mode=json" regex(/^go[._-]?v?(\d+(?:\.\d+)+)[._-]src\.t.+$/i) strategy :json do |json, regex| json.map do |release| next if release["stable"] != true next if release["files"].none? { |file| file["filename"].match?(regex) } release["version"][/(\d+(?:\.\d+)+)/, 1] end end end bottle do sha256 cellar: :any_skip_relocation, arm64_tahoe: "a08dc2f6914ccf3edf0a4e0401595216fb890ffd253ee1147a7241e4f5dc8c15" sha256 cellar: :any_skip_relocation, arm64_sequoia: "a08dc2f6914ccf3edf0a4e0401595216fb890ffd253ee1147a7241e4f5dc8c15" sha256 cellar: :any_skip_relocation, arm64_sonoma: "a08dc2f6914ccf3edf0a4e0401595216fb890ffd253ee1147a7241e4f5dc8c15" sha256 cellar: :any_skip_relocation, sonoma: "b40bc5deb0d8f4a0ed30763b774c294e2033be3af9ae1febc428260bc9080739" sha256 cellar: :any_skip_relocation, arm64_linux: "986db3b38f0108765760294d979cbe758411f9668b099ab852ca1f463d9aa2ca" sha256 cellar: :any_skip_relocation, x86_64_linux: "c44f1e34c0b26a987008b483cf22f188a0c60533277ffcb9485bf66045c82094" end depends_on macos: :monterey # Don't update this unless this version cannot bootstrap the new version. resource "gobootstrap" do checksums = { "darwin-arm64" => "416c35218edb9d20990b5d8fc87be655d8b39926f15524ea35c66ee70273050d", "darwin-amd64" => "e7bbe07e96f0bd3df04225090fe1e7852ed33af37c43a23e16edbbb3b90a5b7c", "linux-arm64" => "fd017e647ec28525e86ae8203236e0653242722a7436929b1f775744e26278e7", "linux-amd64" => "4fa4f869b0f7fc6bb1eb2660e74657fbf04cdd290b5aef905585c86051b34d43", } version "1.22.12" on_arm do on_macos do url "https://go.dev/dl/go#{version}.darwin-arm64.tar.gz" sha256 checksums["darwin-arm64"] end on_linux do url "https://go.dev/dl/go#{version}.linux-arm64.tar.gz" sha256 checksums["linux-arm64"] end end on_intel do on_macos do url "https://go.dev/dl/go#{version}.darwin-amd64.tar.gz" sha256 checksums["darwin-amd64"] end on_linux do url "https://go.dev/dl/go#{version}.linux-amd64.tar.gz" sha256 checksums["linux-amd64"] end end end def install libexec.install Dir["*"] (buildpath/"gobootstrap").install resource("gobootstrap") ENV["GOROOT_BOOTSTRAP"] = buildpath/"gobootstrap" cd libexec/"src" do # Set portable defaults for CC/CXX to be used by cgo with_env(CC: "cc", CXX: "c++") { system "./make.bash" } end bin.install_symlink Dir[libexec/"bin/go*"] # Remove useless files. # Breaks patchelf because folder contains weird debug/test files rm_r(libexec/"src/debug/elf/testdata") # Binaries built for an incompatible architecture rm_r(libexec/"src/runtime/pprof/testdata") # Remove testdata with binaries for non-native architectures. rm_r(libexec/"src/debug/dwarf/testdata") end test do (testpath/"hello.go").write <<~GO package main import "fmt" func main() { fmt.Println("Hello World") } GO # Run go fmt check for no errors then run the program. # This is a a bare minimum of go working as it uses fmt, build, and run. system bin/"go", "fmt", "hello.go" assert_equal "Hello World\n", shell_output("#{bin}/go run hello.go") with_env(GOOS: "freebsd", GOARCH: "amd64") do system bin/"go", "build", "hello.go" end (testpath/"hello_cgo.go").write <<~GO package main /* #include #include void hello() { printf("%s\\n", "Hello from cgo!"); fflush(stdout); } */ import "C" func main() { C.hello() } GO # Try running a sample using cgo without CC or CXX set to ensure that the # toolchain's default choice of compilers work with_env(CC: nil, CXX: nil, CGO_ENABLED: "1") do assert_equal "Hello from cgo!\n", shell_output("#{bin}/go run hello_cgo.go") end end end