1 回答

TA貢獻1847條經驗 獲得超11個贊
在Golang中是別名,當您使用它時,它返回與您的數據類型相同的類型。因此,當您收到此類數據時,它會轉換為字符串。byteuint8json.Marshal[]byte
您需要將 uint8 強制轉換為其他 int 類型或實現Marshaler interface
投
bytes, err := service.GetValue()
value := make([]int8, 0)
for _, v := range bytes {
value = append(value, int8(v))
}
元帥
type CustomType []uint8
func (u CustomType) MarshalJSON() ([]byte, error) {
var result string
if u == nil {
result = "null"
} else {
result = strings.Join(strings.Fields(fmt.Sprintf("%d", u)), ",")
}
return []byte(result), nil
}
func GetValue(c echo.Context) error {
var value CustomType
bytes, err := service.GetValue()
value = bytes
return c.JSON(http.StatusOK, value)
}
- 1 回答
- 0 關注
- 91 瀏覽
添加回答
舉報