Saturday, March 18, 2023
HomeGolangCommon and also typecasting - Obtaining Assistance

Common and also typecasting – Obtaining Assistance

Hi there!
I intend to create an easy technique to transform GO standard kind (int( 8/16/32)/ uint( 8/16/32)) to cgo kind ( C.uint e.t.c.)
I intend to make use of generics yet satisfied an issue with type-casting.
see the adhering to code:

// wrapCType is a common technique to transform GOTYPE (int32/float32 e.t.c.) right into CTYPE (c_int/ c_float e.t.c.).
func wrapCType[CTYPE any, GOTYPE any]( goValue * GOTYPE) (covered * CTYPE, finisher func()) {
if goValue!= nil {
cValue:= CTYPE( any kind of(* goValue)).
covered = && cValue.
finisher = func() {
* goValue = GOTYPE( any kind of( cValue)).
} else {
finisher = func() {}


This does not function because CTYPE and also GOTYPE might be anything, so you can not simply make use of type-conversion phrase structure. You could have the ability to do this with restrictions on CTYPE and also GOTYPE similar to this:

 kind CNumber user interface C.double.

kind GoNumber user interface ~ uint32

func wrapCType[CTYPE CNumber, GOTYPE GoNumber]( goValue * GOTYPE) (covered * CTYPE, finisher func()).

However I’m uncertain; I have not gone to a computer system for a however and also the Go play ground does not function when you import "C"


Most Popular

Recent Comments