[4]byte

Here is my struct

type Lead struct {
  Magic        [4]byte
  Major, Minor byte
  Type         uint16
  Arch         uint16
  Name         string
  OS           uint16
  SigType      uint16
}

I am trying to do the following:

lead := Lead{}
lead.Magic = buffer[0:4]
[]byteuint64[]byte[4]byte

The built in method copy will only copy a slice to a slice NOT a slice to an array.

You must trick copy into thinking the array is a slice

copy(varLead.Magic[:], someSlice[0:4])

Or use a for loop to do the copy:

for index, b := range someSlice {

    varLead.Magic[index] = b

}

Or do as zupa has done using literals. I have added onto their working example.

这篇关于在Golang中,你如何将切片转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!