I have this C code:
uint8_t *data[BUF_SIZE];
data = ...;
// extern void goReadData(uint8_t *data, int bufferSize);
goReadData(data, BUF_SIZE)
And in the GO Code I'm trying to use the data pointer as a GO array or slice, I want to retrieve an []uint8 from the *C.uint8_t. I know the size of the data
//export goReadData
func goReadData(data *C.uint8_t, bufferSize C.int) {
fmt.Printf("Data type %v\n", reflect.TypeOf(data))
// print 1: Data type *main._Ctype_uchar
// Solution 1: GoBytes
// works but really slow (memory copy I think)
goBytes := C.GoBytes(unsafe.Pointer(data), bufferSize)
fmt.Printf("goBytes type %v\n", reflect.TypeOf(goBytes))
// print 2: goBytes type []uint8
// Solution 2: direct pointer
// Really fast, but wrong type at the end
// unsafe.Pointer to the C array
unsafePtr := unsafe.Pointer(data)
// convert unsafePtr to a pointer of the type *[1 << 30]C.uint8_t
arrayPtr := (*[1 << 30]C.uint8_t)(unsafePtr)
// slice the array into a Go slice, with the same backing array
// as data, making sure to specify the capacity as well as
// the length.
length := int(bufferSize)
slice := arrayPtr[0:length:length]
fmt.Printf("Direct slice type %v\n", reflect.TypeOf(slice))
//Print 3: Direct type []main._Ctype_uchar
}
How could I do to recover an []uint8 instead of []main._Ctype_uchar with the second solution? Or do you have another solution to do that without a bytes copy?
Sorry guys, I found my own mistake:
// convert unsafePtr to a pointer of the type *[1 << 30]C.uint8_t
arrayPtr := (*[1 << 30]C.uint8_t)(unsafePtr)
==> to
// convert unsafePtr to a pointer of the type *[1 << 30]uint8
arrayPtr := (*[1 << 30]uint8)(unsafePtr)
Problem solved!
Thanks ;)
Related
I am passing in a string array and an empty integer array into a function. The point of the function is to convert each element of the string array to an integer and store that into the integer array. When I print the integer array from within the function itself, everything is fine. However, when I try to print the integer array outside of the function, it prints an empty array.
employeeDataInt is the integer array, and employeeDataString is the string array.
I apologize if this is a dumb question but I am new to go. Thanks
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func strToInt(employeeDataString []string, emplyoeeDataInt []int) []int {
for _, i := range employeeDataString[2:] {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
employeeDataInt = append(employeeDataInt, j)
fmt.Println(employeeDataInt) //this prints out the appropriate array
}
return employeeDataInt
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter file name: ")
fileName, err := reader.ReadString('\n')
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
fileName = strings.TrimSuffix(fileName, "\n")
file, err := os.Open(fileName)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var employeeLine []string
for scanner.Scan() {
employeeLine = append(employeeLine, scanner.Text())
}
file.Close()
var employeeDataString = []int{}
for _, employee := range employeeLine {
employeeDataString := strings.Split(employee, " ")
strToInt(employeeDataString, employeeDataInt)
fmt.Println(playerData2) //this is outputting just `[]`
}
}
You aren't taking the value of the array and thus the Slice you passed into the function might or might not be updated correctly.
strToInt(employeeDataString, employeeDataInt)
// should be
employeeDataInt = strToInt(employeeDataString, employeeDataInt)
And while at it, you are never assigning playerData2. So fmt.Println(playerData2) will always be [].
But aside from that there are some subtle issues with your usage of Arrays/Slices here:
First the difference between Slices and Arrays:
Go does not allow you to directly work with Arrays.
Unless they have a fixed length ([3]int{} or []int{1,2,3]) you aren't actually looking at an array but at a Slice ([]int).
The slice is just a pointer to an array (along with it's capacity and some other info) and it essentially allows Go to safely muck around with arrays because you never grow an existing array (the size of an array is fixed at initialization). So you can never append to an array.
What Go does to give you the illusion of appending to an array is having a larger than required underlying array, and the Slice controls the access to that array. So if the underlying array has a capacity of 5 and you already stored 3 items in it you can do 2 append operations without having to allocate a new array and copy the existing array elements to the new memory location.
So when you are passing a []int you are actually passing an array pointer (by value) around.
This leads to the next gotcha in your code: The use of append.
As mentioned above, append takes a Slice, looks at the underlying array and how much space is actually left and then adds to it or allocates a new array. If a new array is allocated append returns a new slice that points to the new array.
So calling:
foo := []{1,2,3}
append(foo, 4)
append(foo, 5)
append(foo, 6)
fmt.Print(foo)
// => might return 1,2,3,4,5
You always have to take the return value of append otherwise you risk still referencing the "old" slice that didn't get the new items appended.
So the correct way to grow a Slice, or work with Slices in general is to keep in mind that: Slices are passed by value, so always keep updating your variables with the return values of Slice modifying functions.
There are a few issues in your code:
You're discarding the return value of strToInt.
You're trying to utilize employeeDataInt in main but it is undefined there (which should be causing a compile error, not a runtime issue).
You're declaring employeeDataString twice, in two different scopes in main (inside and outside the for loop), with two different types ([]string and []int). The outer-scoped variable is unused, so should also be causing a compile error.
You're printing playerData2 which is never defined or used - again, this should be causing a compiler error, not incorrect behavior.
Given there were compile errors in the code, either some crucial code was missing from your post, or you did not notice/mention the compile errors.
The correct code within main would be:
var employeeDataInt []int // Seems like you just have the wrong variable name here
for _, employee := range employeeLine {
employeeDataString := strings.Split(employee, " ")
// You're missing the assignment here
employeeDataInt = strToInt(employeeDataString, employeeDataInt)
fmt.Println(employeeDataInt) // This was referencing the wrong variable
}
How can I access a slice defined inside the struct?
type Car struct {
Year int
Name string
Type []int
}
//Accessing "Type" array field as below causes error: array out of range.
Car.Type[0] = 12
Car.Type[1] = 15
Car.Type[2] = 11
You mistake slice for array. It must be:
type Car struct {
Year int
Name string
Type [3]int // <---
}
See running code
You should read this tour: https://tour.golang.org/moretypes/6
You can't directly access a slice field if it's not been initialised. You're defining a struct to have 3 fields: Year of type int, this is a simple value that is part of the struct. Same goes for Name. The Type field, however, is a slice. A slice is a reference type. That means it's essentially a hidden struct (called the slice header) with underlying pointer to an array that is allocated dynamically for you. This underlying pointer is, at the time you initialise your variable, nil.
type Car struct {
Year int
Name string
Type []int
}
Can be seen as:
type Car struct {
Year int
Name string
Type struct{
type: "int",
array *[]T
}
}
Not exactly, but you get the idea. When you write:
c := Car{}
All you've allocated is the int, string and the slice header. You must, therefore initialise the slice first:
c := Car{
Year: 2018,
Name: "vroom",
Type: []int{
1, 2, 3,
},
}
There are many ways to initialise the slice, of course. You don't have to set the values just yet, but you could, for example, allocate and initialise the memory you need in one go:
c.Type = make([]int, 3) // allocates an initialised 3 elements in the slice to 0
you can also allocate but not initialise the slice by specifying the capacity (this is useful to avoid reallocating and moving the slice around too often):
c.Type = make([]int, 0, 3)
Alternatively, you can have the runtime do it all for you, using append:
c.Type = append(c.Type, 1, 2, 3)
Some examples here
A bit more background. Slices and maps work similarly, broadly speaking. Because they are reference types, that rely on pointers internally, it's possible for functions that have a slice as a return type to return nil for example. This doesn't work for functions returning an int:
func nilSlice() []int {
return nil
}
Because the return type is a slice, what this function will return is, essentially, an empty slice. Accessing it will result in the same error you occurred: index out of range.
Trying to return nil from a function like this won't even compile:
func nilInt() int {
nil
}
The resulting error will say something like "Can't use nil as type int". Treat slices as pointer types: they need to be safely initialised before use. Always check their length, and if you need to optimise, look at how the builtin append function is implemented. It'll just exponentially grow the capacity of the underlying array. Something that you may not always want. It's trivial to optimise this sort of stuff
You are confusing Slices and Array. Slices are like dynamic arrays. The way you have defined the slice, their index is not defined until they are appended. For the above code:
type Car struct {
Type []int
}
var car Car
car.Type = append(car.Type, 12)
car.Type = append(car.Type, 15)
car.Type = append(car.Type, 11)
Also, Car in your case is a type of object not a object itself. I have declared object car of type Car.
How do I convert this C (array) type:
char my_buf[BUF_SIZE];
to this Go (array) type:
type buffer [C.BUF_SIZE]byte
? Trying to do an interface conversion gives me this error:
cannot convert (*_Cvar_my_buf) (type [1024]C.char) to type [1024]byte
The easiest and safest way is to copy it to a slice, not specifically to [1024]byte
mySlice := C.GoBytes(unsafe.Pointer(&C.my_buff), C.BUFF_SIZE)
To use the memory directly without a copy, you can "cast" it through an unsafe.Pointer.
mySlice := (*[1 << 30]byte)(unsafe.Pointer(&C.my_buf))[:int(C.BUFF_SIZE):int(C.BUFF_SIZE)]
// or for an array if BUFF_SIZE is a constant
myArray := *(*[C.BUFF_SIZE]byte)(unsafe.Pointer(&C.my_buf))
To create a Go slice with the contents of C.my_buf:
arr := C.GoBytes(unsafe.Pointer(&C.my_buf), C.BUF_SIZE)
To create a Go array...
var arr [C.BUF_SIZE]byte
copy(arr[:], C.GoBytes(unsafe.Pointer(&C.my_buf), C.BUF_SIZE))
I have an array of bytes and another array of array of bytes.
type
TByteArray = array of byte;
TArraykearray = array of array of byte;
function TFRTConnection1.GetBytes(value: integer): TBytearray;
begin
SetLength(Result, SizeOf(value));
Move(value, Result[0], SizeOf(value));
end;
Function TFRTConnection1.addco(point: TPoint) : Tarraykearray;
var
result1 : Tarraykearray;
begin
setLength(Result1,10);
Result1[0] := getBytes(1);
Result1[1] := 1;
....
Result := result1;
end;
When i use Result1[0] := getBytes(1); i get incompatible types error.
I do not want to make TArraykearray as array of Tbytearray because that is an array of array on the server side of the application for which I do not have access to.
Is there another possibility?
You cannot assign a TByteArray to an array of byte, the compiler treats them as separate types even though internally they are compatible. You need to change TArraykearray to use TByteArray instead:
type
TByteArray = array of byte;
TArraykearray = array of TByteArray;
Or else you will have to use a type-cast when assigning the array:
Function TFRTConnection1.addco(point: TPoint) : Tarraykearray;
type
PByteArray = ^TByteArray;
var
Result1 : TArraykearray;
begin
SetLength(Result1,10);
PByteArray(#Result1[0])^ := getBytes(1);
....
end;
BTW: Result1[1] := 1; will not compile either, as you cannot assign a single integer to an array.
I would like to write a program that receive a array (of string, int, or whatever) and create another array of the same type contain only the first element.
For example:
for a array of strings arr := []string("hello", "world")
my output would be arr2 := []string(arr[0]);
I can't use the copy function because to do that, i would have to create(make) a new slice for it. And in this case, i still have to discover which type the first array is (string, int, bool, and so on...)
Maybe I could use the reflect.TypeOf() but i would still not know how to use that information to create the same type of slice or array.
i'm not considering to use conditionals for that.
For example:
if reflect.TypeOf(arr) == []int {
arr := []int(arr[0])
} else if reflect.TypeOf(arr) == []string
arr := []string(arr[0])
} ...
I would be glad get a help on there.
Thanks in advance.
You could just subslice it in place:
s2 := s1[0:1]
But if you really need to create a new slice, you can do it like this:
func f(s interface{}) interface{} {
v := reflect.ValueOf(s)
t := v.Type()
res := reflect.MakeSlice(t, 1, 1)
res.Index(0).Set(v.Index(0))
return res.Interface()
}
Playground: http://play.golang.org/p/w1N3pgvAwr.