我有大致這樣的東西
type Guid [16]byte
type Payload struct {
....
SthGuid [17]byte
}
func (h *...) Get(guid Guid) (... error) {
}
我想用 SthGuid 的最后 16 個位元組呼叫 Get。例如,
Get(PayloadInstance.SthGuid[1:16]))
無法將 SthGuid[1:16]([]byte 型別的值)轉換為 Guid
我正在嘗試呼叫 SthGuid[1:] 對第一個位元組進行切片并將最后 16 個位元組用作輸入引數。那樣不行。
uj5u.com熱心網友回復:
您可以使用正確的型別復制陣列,例如:
var guid [16]byte
copy(guid[:], SthGuid[1:16])
Get(guid)
或者,作為 Go 1.17,您可以嘗試使用陣列轉換:
https://tip.golang.org/ref/spec#Conversions_from_slice_to_array_pointer
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422092.html
標籤:
