github.com/google/capslock@v0.2.3-0.20240517042941-dac19fc347c0/testpkgs/usecgo/usecgo.go (about) 1 // Copyright 2023 Google LLC 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file or at 5 // https://developers.google.com/open-source/licenses/bsd 6 7 // Package usecgo is used for testing. It contains a C function and a Go 8 // function which calls it via cgo, and some functions which call type 9 // conversion functions from the "C" pseudo-package. 10 package usecgo 11 12 // int acfunction() { return 42; } 13 import "C" 14 import "unsafe" 15 16 // Foo calls a C function using cgo. 17 func Foo() int { 18 return int(C.acfunction()) + 1 19 } 20 21 // CallCString is a test function that calls C.CString. 22 func CallCString() { 23 s := "abc" 24 var c *C.char = C.CString(s) 25 _ = c 26 } 27 28 // CallCBytes is a test function that calls C.CBytes. 29 func CallCBytes([]byte) { 30 b := []byte{1, 2, 3} 31 var u unsafe.Pointer = C.CBytes(b) 32 _ = u 33 } 34 35 // CallGoString is a test function that calls C.GoString. 36 func CallGoString(*C.char) { 37 var c C.char 38 var s string = C.GoString(&c) 39 _ = s 40 } 41 42 // CallGoStringN is a test function that calls C.GoStringN. 43 func CallGoStringN(*C.char, C.int) { 44 var c C.char 45 var s string = C.GoStringN(&c, C.int(1)) 46 _ = s 47 } 48 49 // CallGoBytes is a test function that calls C.GoBytes. 50 func CallGoBytes(unsafe.Pointer, C.int) { 51 var u unsafe.Pointer 52 var b []byte = C.GoBytes(u, C.int(1)) 53 _ = b 54 }