Go version
go version go1.23.1 linux/amd64
Output of go env
in your module/workspace:
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/username/.cache/go-build'
GOENV='/home/username/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/username/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/username/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/username/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3506538111=/tmp/go-build -gno-record-gcc-switches'
What did you do?
I want to write a function that converts C.char from Package A to C.char from Package B I wrote function like that
func ConvPointer[RESULT, SOURCE *any](ptr SOURCE) RESULT {
return (RESULT)(unsafe.Pointer(ptr))
}
What did you see happen?
when calling this function I get:
../../typewrapper/type_wrapper.go:66:37: *_Ctype_char does not satisfy *any (*_Ctype_char missing in *any)
What did you expect to see?
any
as the name suggests means anything, so why C.char
is not anything? Is there a reason for that?
Comment From: gabyhelp
Related Issues and Documentation
- cgo: cannot use C reference variable created by one package in another #59606 (closed)
- cmd/cgo: cannot convert from signed char *_Ctype_schar to char *_Ctype_char #26655 (closed)
- cmd/cgo: compiler complains about `C.free(*C.char)` but is happy with `defer C.free(*C.char)` #17562 (closed)
- cmd/cgo: C.* cannot be used for type constraints #52542 (closed)
- cmd/cgo: don't accept mangled C names in Cgo programs #28721 (closed)
- cmd/cgo: cgo treats pointer to fixed-length array as unsafe.Pointer when CC=gcc #29909 (closed)
- cmd/cgo: pointer to incomplete C type is mangled when passed through interface type and generic type assert #51733 (closed)
- x/tools/gopls: definition request fails on method with receiver type of form C.T (cgo) #59723 (closed)
- C.free(unsafe.Pointer(cs)) returns "could not determine kind of name for C.free" #39871 (closed)
- CGO C.Bytes Not Work As Expected #49670 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: ianlancetaylor
That is how the language works. The type any
is a name for the empty interface type interface{}
. A value of type *C.char
is a pointer to a C.char
. A value of type *any
is a pointer to a interface{}
. The two are not compatible. See https://go.dev/doc/faq#convert_slice_of_interface, which is written in terms of []any
but applies equally well to *any
.
You can write your function as
func ConvPointer[ER, ES any, RESULT *ER, SOURCE *ES](ptr SOURCE) RESULT {
return (RESULT)(unsafe.Pointer(ptr))
}
Closing because this is not a bug.
Comment From: gucio321
oh ok, I missed that, thank you!!