Source file src/runtime/cgo/library_ext_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  //go:build goexperiment.cgobindings && (unix || windows)
     6  
     7  package cgo_test
     8  
     9  import (
    10  	"runtime"
    11  	"runtime/cgo"
    12  	"testing"
    13  )
    14  
    15  // myLib is declared outside package cgo, as a //cgo:dynload type would be.
    16  type myLib struct {
    17  	cgo.Library
    18  	name string // user fields are allowed
    19  }
    20  
    21  func TestOpenEmbedded(t *testing.T) {
    22  	var path string
    23  	switch runtime.GOOS {
    24  	case "linux":
    25  		path = "libc.so.6"
    26  	case "darwin", "ios":
    27  		path = "/usr/lib/libSystem.B.dylib"
    28  	case "windows":
    29  		path = "msvcrt.dll"
    30  	default:
    31  		t.Skipf("no known C library path on %s", runtime.GOOS)
    32  	}
    33  	// Open infers PT = *myLib; the constraint is satisfied by the
    34  	// unexported method promoted from the embedded cgo.Library.
    35  	lib, err := cgo.Open[myLib](path)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	var _ *myLib = lib
    40  }
    41  

View as plain text