I am trying to simulate the memcpy from C in Go using unsafe.Pointer
.
I have to map a string into a struct of strings in the following way:
package main
import (
"fmt"
"unsafe"
)
type myMessage struct {
Field1 [30]string
Field2 [2]string
Field3 [4]string
Field4 [1]string
Field5 [1]string
}
func main() {
var inputString string = "Abcdefghi"
inputPtr := &inputString
unsafePtr := unsafe.Pointer(inputPtr)
messPtr := (*myMessage)(unsafePtr)
var messageString myMessage = *messPtr
fmt.Println(messageString)
}
The result is as following:
./test
{[Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi Abcdefghi ] [Abcdefghi ] [Abcdefghi Abcdefghi ] [Abcdefghi] []}
i.e. the code is copying inputString
in each position of the final struct.
Why is the inputString
duplicated?