我有這個結(jié)構(gòu):// Nearby whatevertype Nearby struct { id int `json:"id,omitempty"` me int `json:"me,omitempty"` you int `json:"you,omitempty"` contactTime string `json:"contactTime,omitempty"`}然后我稱之為:strconv.Itoa(time.Now())像這樣:s1 := Nearby{id: 1, me: 1, you: 2, contactTime: strconv.Itoa(time.Now())}但它說:> cannot use time.Now() (type time.Time) as type int in argument to> strconv.Itoa有誰知道那是什么?我想在這里將一個 int 轉(zhuǎn)換為一個字符串。
1 回答

撒科打諢
TA貢獻1934條經(jīng)驗 獲得超2個贊
有誰知道那是什么?我想在這里將一個 int 轉(zhuǎn)換為一個字符串。
時間類型不等同于 int。如果您需要的是字符串表示,typeTime有一個String()方法。
下面的示例代碼(也可作為可運行的 Go Playground代碼段提供):
package main
import (
"fmt"
"time"
)
// Nearby whatever
type Nearby struct {
id int
me int
you int
contactTime string
}
func main() {
s1 := Nearby{
id: 1,
me: 1,
you: 2,
contactTime: time.Now().String(), // <-- type Time has a String() method
}
fmt.Printf("%+v", s1)
}
希望這可以幫助。干杯,
- 1 回答
- 0 關(guān)注
- 203 瀏覽
添加回答
舉報
0/150
提交
取消