Source file src/internal/godebug/link_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package godebug_test
     6  
     7  import (
     8  	"bytes"
     9  	"internal/testenv"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  // Test that using a GODEBUG setting does not link in the runtime metrics implementation.
    16  func TestLinkerGC(t *testing.T) {
    17  	if testing.Short() {
    18  		t.Skip("skipping in short mode")
    19  	}
    20  	testenv.MustHaveGoBuild(t)
    21  	t.Parallel()
    22  
    23  	dir := t.TempDir()
    24  	src := filepath.Join(dir, "main.go")
    25  	program := []byte(`package main
    26  
    27  import _ "net"
    28  
    29  func main() {}
    30  `)
    31  	if err := os.WriteFile(src, program, 0644); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	exe := filepath.Join(dir, "main.exe")
    36  	cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, src)
    37  	if out, err := cmd.CombinedOutput(); err != nil {
    38  		t.Fatalf("build: %v\n%s", err, out)
    39  	}
    40  
    41  	cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe)
    42  	nm, err := cmd.CombinedOutput()
    43  	if err != nil {
    44  		t.Fatalf("nm: %v\n%s", err, nm)
    45  	}
    46  	if want := []byte("internal/godebug.(*Setting).Value"); !bytes.Contains(nm, want) {
    47  		t.Fatalf("expected symbol %q not found", want)
    48  	}
    49  	if bad := []byte("runtime.initMetrics"); bytes.Contains(nm, bad) {
    50  		t.Fatalf("unexpected symbol %q found", bad)
    51  	}
    52  }
    53  

View as plain text