Tuesday, May 9, 2023
HomeGolangCommon and also typecasting - Obtaining Aid

Common and also typecasting – Obtaining Aid


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

// wrapCType is a common approach 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 type of(* goValue)).
covered = && cValue
. finisher = func() {
* goValue = GOTYPE( any type of( cValue)).
}
} else {
finisher = func() {}
}

return.
}


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 may have the ability to do this with restrictions on CTYPE and also GOTYPE similar to this:

 kind CNumber user interface 

kind GoNumber user interface 


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

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

thanks for your response!

unfortunately, it generates the list below mistakes:

./ type_wrapper. go:33:2: void use|(underlying kind of _ Ctype_char is int8)
./ type_wrapper. go:33:12: void use|(underlying kind of _ Ctype_short is int16)
./ type_wrapper. go:33:23: void use|(underlying kind of _ Ctype_int is int32)
./ type_wrapper. go:33:32: void use|(underlying kind of _ Ctype_long is int64)
./ type_wrapper. go:34:3: void use|(underlying kind of _ Ctype_uchar is uint8)
./ type_wrapper. go:34:14: void use|(underlying kind of _ Ctype_ushort is uint16)
./ type_wrapper. go:34:26: void use|(underlying kind of _ Ctype_uint is uint32)
./ type_wrapper. go:34:36: void use|(underlying kind of _ Ctype_ulong is uint64).

additionally, might I ask what is the |?

oh, ok, it appears that I have actually taken care of that :smile:

 ype CNumber user interface 
C.float

kind GoNumber user interface 


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

return.
}

nevertheless, goland whines regarding that conversion

RELATED ARTICLES

Most Popular

Recent Comments