Copying struct value to a interface{ } in golang - go

I will like to understand why when I copy a struct value into an interface it behaves like it does. In this code can someone help me understand why when I copy the value from mySlice into foo3 it behaves different than the other copies?
package main
import (
"fmt"
"unsafe"
)
type SliceOfInt []int
// when passing a slice to a method you are passing this data. Lets prove it
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
// Print the header of a slice. Its Data. Len and Cap
func GetHeaderOfSliceOfInt(s unsafe.Pointer) *SliceHeader {
// this header needs to point to &mySlice but compiler will not let us. we have to use unsafe pointers
var header *SliceHeader
pointerToMySlice := s
header = ((*SliceHeader)(pointerToMySlice))
return header
}
func main() {
// On go everything is passed by coping values. When you pass a slice to a function you are passing this:
// reference: https://stackoverflow.com/a/39993797/637142
/*
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
*/
// create a new slice
var mySlice SliceOfInt = make([]int, 0)
mySlice = append(mySlice, 123456789) // append this number to mySlice
// now we have a slice with len:1 and capacity:1. Lets prove it
header := GetHeaderOfSliceOfInt(unsafe.Pointer(&mySlice))
fmt.Println(*header)
// this prints: {824635465728 1 1}
// this means that on memory address 824635465728 there is an array with cap:1 and len:1
// copy that header to someOtherSlice
someOtherSlice := mySlice
header = GetHeaderOfSliceOfInt(unsafe.Pointer(&someOtherSlice))
fmt.Println(*header)
// prints the same value {824635465728 1 1}
// anyways if I go to address 824635465728 on my computer I shoul dbe able to find integer 123456789
pointerToInteger := unsafe.Pointer((*header).Data)
var integerVal *int = ((*int)(pointerToInteger))
fmt.Println(*integerVal)
// if I copy like this, it will print the correct header {824635465728 1 1}
foo1 := mySlice
fmt.Println(*GetHeaderOfSliceOfInt(unsafe.Pointer(&foo1)))
// copy like this will also print the correct header {824635465728 1 1}
foo2 := foo1
fmt.Println(*GetHeaderOfSliceOfInt(unsafe.Pointer(&foo2)))
// If I copy like this it will print the incorrect header. Why?
var foo3 interface{} = mySlice
fmt.Println(*GetHeaderOfSliceOfInt(unsafe.Pointer(&foo3)))
// this last line prints {4746976 824635330392 0}
}
The output of the program is:
{824635465728 1 1}
{824635465728 1 1}
123456789
{824635465728 1 1}
{824635465728 1 1}
{4746976 824635330392 0}
Edit
I know that if I cast foo3 as: foo3.(SliceOfInt) it will work. But why is that?

An interface type, empty or not, is a type in its own right. It has its own memory representation and it is a legitimate member of Go's type system.
An interface value, and the value wrapped in that interface, are not one and the same.
The variables foo1 and foo2 have the same type and value as the mySlice variable. But the variable foo3 has a different type, therefore also a different value. And yes, the dynamic type and value are the same as mySlice but the static type and value are not.
An interface value is NOT represented in memory by a structure that's compatible with the three-field SliceHeader and therefore it is wrong, not only semantically, to try to take the slice header off of an interface value. Instead, an interface value is represented by a 2-field structure (that's why in your attempt the third field is 0). The first field points to the type information of the wrapped value, the second field points to the data of the wrapped value.
Something like this:
type iface struct {
typ uintptr
data uintptr
}
And you can test this by doing this:
x := (*iface)(unsafe.Pointer(&foo3))
s := (*SliceHeader)(unsafe.Pointer(x.data))
fmt.Printf("%+v\n", x)
fmt.Printf("%+v\n", s)
https://go.dev/play/p/2KUgCU8h7O7
Also, consider reading this: https://research.swtch.com/interfaces.

Related

use unsafe pointer to get value from a []string

I am trying to learn how pointers work on go. Why is the following example not working?
package main
import (
"fmt"
"unsafe"
)
type SliceOfStrings []string
// function that creates an slice of []string
// returns interface{} cause I am interested on learning how pointers work
func Create() interface{} {
var mySlice1 SliceOfStrings = make([]string, 0)
mySlice1 = append(mySlice1, "str1")
mySlice1 = append(mySlice1, "str2")
// return a slice with as ["str1","str2"]
return mySlice1
}
func main() {
x := Create()
// 0xc000021940
fmt.Printf("address of x is %p \n", &x)
// get unsafe pointer to address of x
// unsafe pointer. Prints 0xc000021940
p1 := unsafe.Pointer(&x)
fmt.Println(p1)
// unsigned pointer. Prints 824633858368
p2 := uintptr(p1)
fmt.Println(p2)
// prints same value as p1 0xc000021940
p3 := unsafe.Pointer(p2)
fmt.Println(p3)
// Make p4 point to same address as 0xc000021940
p4 := (*SliceOfStrings)(p3)
//fmt.Println(p4)
// why this does not print "str1" ??
fmt.Println((*p4)[0])
// I get error: runtime error: invalid memory address or nil pointer dereference
}
Create() returns a value of type interface{}, so type of x is interface{}, so type of &x is *interface{}, and not *SliceOfStrings. So x points to an interface{} value and not to a SliceOfStrings value!
If you type assert SliceOfStrings from the return value of Create(), it works:
x := Create().(SliceOfStrings)
Also add runtime.KeepAlive(x) at the end of your main(), because if you don't refer to x anymore, it can be garbage collected at any time.
With this change it works and outputs str1. Try it on the Go Playground.
In general, stay away from package unsafe as much as possible. You can learn and use pointers without package unsafe. Only think of unsafe as a last-last resort!
I was able to understand why this happens:
package main
import (
"fmt"
"unsafe"
)
type SliceOfStrings []string
// when passing a slice to a method you are passing this data. Lets prove it
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
func main() {
// On go everything is passed by coping values. When you pass a slice to a function you are passing this:
// reference: https://stackoverflow.com/a/39993797/637142
/*
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
*/
// create a new slice
var mySlice1 SliceOfStrings = make([]string, 0)
// when appending we need to keep the new content that is why we reasig it
mySlice1 = append(mySlice1, "str1")
mySlice1 = append(mySlice1, "str2")
// in other words this will make no sense:
// _ = append(mySlice1, "str3") // doing this will lose the new header value
// lets see the content of header mySlice
var pointerToMySlice1 = unsafe.Pointer(&mySlice1)
var header *SliceHeader = ((*SliceHeader)(pointerToMySlice1))
fmt.Println(*header)
// {824634220576 2 2}
// lets copy that header to another slice
var copy SliceOfStrings = mySlice1
var pointerToCopy = unsafe.Pointer(&copy)
header = ((*SliceHeader)(pointerToCopy))
fmt.Println(*header)
// prints the same thing
// {824634220576 2 2}
// now lets do the same thing but with an interface
var copy2 interface{} = mySlice1
var pointerToCopy2 = unsafe.Pointer(&copy2)
header = ((*SliceHeader)(pointerToCopy2))
fmt.Println(*header)
// this prints!
// {4845280 824634375976 0}
// I dont understand why this happens. But this is the reason why the example does not work
// I was trying to access an array from memory address 4845280 that is wrong
// now lets do what izca told us
var copy3 interface{} = mySlice1
tmp := (copy3).(SliceOfStrings)
var pointerToCopy3 = unsafe.Pointer(&tmp)
header = ((*SliceHeader)(pointerToCopy3))
fmt.Println(*header)
// this prints!
// {824634220576 2 2}
// that is the correct value
// lets try to get the array from that memory address (824634220576)
pointerToActualArray := unsafe.Pointer(header.Data)
// lets cast that to (*[2]string)
var pointerFinal *[2]string = ((*[2]string)(pointerToActualArray))
// now print the first value
fmt.Println((*pointerFinal)[0])
// prints str1
}

How I can update struct's array field value via reflect in Go?

I want to edit struct fields' value as interface{} parameters.
I done almost, but when struct has array field, I couldn't modify that.
I tried to find proper function with array value, but I couldn't find that.
Here is my code.
edit_struct_via _interface.go
package main
import (
"reflect"
"fmt"
)
type myStruct struct {
Name [8]byte
TID [4]byte
Chksum uint16
}
func main() {
var s = new(myStruct)
name := []byte("Mike")
tid := []byte{0x01, 0x02, 0x03, 0x05}
setArrayField(name, s.Name[:])
setArrayField(tid, s.TID[:])
s.Chksum = 0x0101
myObj := reflect.ValueOf(s).Elem()
typeOfMyObj := myObj.Type()
for i := 0; i < myObj.NumField(); i++ {
f := myObj.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfMyObj.Field(i).Name, f.Type(), f.Interface())
}
newName := []byte("Emil")
myObj.Field(0).SetBytes(newName[:])
// I trying to change array field at here, but panic occur here.
// Because SetBytes must been called on slice, but myObj.Field(0) is array.
myObj.Field(2).SetUint(99) // It works.
fmt.Println("s is now: ", *s)
}
func setArrayField(src []byte, target []byte) {
// retun if src is bigger than target
if len(src) > len(target){
return
}
for index, item := range src {
target[index] = item
}
}
When I run above code, I get
0: Name [8]uint8 = [77 105 107 101 0 0 0 0]
1: TID [4]uint8 = [1 2 3 5]
2: Chksum uint16 = 257
panic: reflect: call of reflect.Value.SetBytes on array Value
goroutine 1 [running]:
reflect.flag.mustBe(...)
/usr/local/go/src/reflect/value.go:208
reflect.Value.SetBytes(0x4a6180, 0xc000098010, 0x191, 0xc000098078, 0x4, 0x4)
/usr/local/go/src/reflect/value.go:1557 +0x140
main.main()
/home/hornet/go/src/gateway-golang/edit_struct_via _interface.go:33 +0x522
exit status 2
I'm using Ubuntu 18.04 and go1.13 linux/amd64
The documentation for SetBytes says:
SetBytes sets v's underlying value. It panics if v's underlying value is not a slice of bytes.
The underling value is an array as stated in the panic messge.
Fix by copying the bytes to the array:
reflect.Copy(myObj.Field(0), reflect.ValueOf(newName))
Run it on the playground.
SetBytes works on slices. Name is not a slice, it is an array [8]byte.
There are important differences between slices and arrays, and you can't really use one for the other. A slice has a reference to an array, when you pass slices around, you pass the reference to the backing array. An array is like a struct, when you pass an array to functions, you pass copies of that array, not a reference to it.
You can use reflect.Copy to copy bytes:
reflect.Copy(myObj.Field(0), reflect.ValueOf(newName))

golang reflection to set first value of a struct to its zero value

go playground: https://play.golang.org/p/ck3PtydW3YT
I have a struct like:
type Input struct {
InputA *InputA
InputB *InputB
InputC *InputC
}
I'm trying to use reflection to set the first value (in this case, *InputA) to its zero value (&InputA{}), but it's not working:
actionInput = Input{}
v := reflect.ValueOf(actionInput)
i := 0
typ := v.Field(i).Type()
inputStruct := reflect.New(typ).Elem().Interface()
reflect.ValueOf(&actionInput).Elem().Field(i).Set(reflect.ValueOf(inputStruct))
I'm guessing this is because it's a pointer, but I'm not sure how to work around that
The code below should work. If the field is a pointer, it creates an instance of the type pointed to by that pointer, and sets that.
typ := v.Field(i).Type()
var inputStruct reflect.Value
if typ.Kind()==reflect.Ptr {
inputStruct=reflect.New(typ.Elem())
} else {
inputStruct = reflect.New(typ).Elem()
}
reflect.ValueOf(&actionInput).Elem().Field(i).Set(inputStruct)

Using reflection to populate a pointer to a struct

I'd like to iterate over the fields in a struct and prompt for string values to string fields, doing this recursively for fields that are pointers to structs.
Currently this is what I've tried, but I get an error when trying to set this value in the pointer's string field.
package main
import (
"fmt"
"reflect"
)
type Table struct {
PK *Field
}
type Field struct {
Name string
}
func main() {
PopulateStruct(&Table{})
}
func PopulateStruct(a interface{}) interface {} {
typeOf := reflect.TypeOf(a)
valueOf := reflect.ValueOf(a)
for i := 0; i < typeOf.Elem().NumField(); i++ {
switch typeOf.Elem().Field(i).Type.Kind() {
case reflect.String:
fmt.Print(typeOf.Elem().Field(i).Name)
var s string
fmt.Scanf("%s", &s)
valueOf.Elem().Field(i).SetString(s)
case reflect.Ptr:
ptr := reflect.New(valueOf.Elem().Field(i).Type())
PopulateStruct(ptr.Elem().Interface())
valueOf.Elem().Field(i).Set(ptr)
}
}
}
Expecting the return value to include an initialised struct with the pointers string field set.
Getting an error when setting the pointer's string field.
panic: reflect: call of reflect.Value.Field on zero Value
I dropped your code as-is into the Go Playground and it doesn't build because PopulateStruct is declared as returning interface{} but does not actually return anything. Removing the declared return type produces the panic you mention.
This is because at entry to the outer PopulateStruct call, you have a valid pointer, pointing to a zero-valued Table. A zero-valued Table has one element: a nil pointer in it of type *Field. Your loop therefore runs once and finds a reflect.Ptr, as you expected. Adding more diagnostic print messages helps see what's happening:
fmt.Printf("PopulateStruct: I have typeOf=%v, valueOf=%v\n", typeOf, valueOf)
for i := 0; i < typeOf.Elem().NumField(); i++ {
switch typeOf.Elem().Field(i).Type.Kind() {
// ... snipped some code ...
case reflect.Ptr:
ptr := reflect.New(valueOf.Elem().Field(i).Type())
fmt.Println("after allocating ptr, we have:", ptr.Type(), ptr,
"but its Elem is:", ptr.Elem().Type(), ptr.Elem())
This prints:
PopulateStruct: I have typeOf=*main.Table, valueOf=&{<nil>}
after allocating ptr, we have: **main.Field 0x40c138 but its Elem is: *main.Field <nil>
Given the way PopulateStruct itself is constructed, we must actually allocate a real Field instance now, before calling PopulateStruct. We can do this with:
p2 := ptr.Elem()
ptr.Elem().Set(reflect.New(p2.Type().Elem()))
(code borrowed from json.Unmarshal). Now we can fill in this Field, which has one field named Name of type String.
The overall strategy here is not that great, in my opinion: filling-in probably should take a generic pointer, not specifically a pointer-to-struct pointer. You can then emulate the indirect function in the json unmarshaller. However, the addition of these two lines—creating the target object and making the allocated pointer point to it—suffices to make your existing code run.
(Alternatively, you could just create and return a whole instance from scratch, in which case all you need is the type—but I'm assuming you have a pattern in which only some fields are nil.)
Here's the complete Go Playground example. I made a few other changes as there's nothing to scan from when using the playground.

Why are Slices insides structs "passed by reference" when passed into functions in Go?

package main
import "fmt"
func main() {
a := SomeType{myslice: []int{1, 2, 3}, decimal: 2.33}
for _, i := range a.myslice {
fmt.Println(i)
}
fmt.Println(a.decimal)
addOne(a)
for _, i := range a.myslice {
fmt.Println(i)
}
fmt.Println(a.decimal)
}
type SomeType struct {
myslice []int
decimal float32
}
func addOne(s SomeType) {
s.myslice[0]++
s.decimal += 1.2
}
The output for the code above is:
1
2
3
2.33
2
2
3
2.33
Even though i have not passed the SomeType object a by reference the myslice field is being modified in the original object. Why is this happening? Is there anyway to pass the entire object by value without having to create a copy of the original object?
The slice is not really being passed by reference; if you append to it in addOne, its length will not change. But a slice contains a reference (or pointer) to its backing array. So when you copy a slice, the new one shares the same backing array with the old one.
The fact that the slice is inside a struct doesn't make any difference. You would see the same thing if you changed addOne to just take a slice instead of the whole struct.

Resources