1 回答

TA貢獻1797條經驗 獲得超4個贊
在 Go 中,不存在像 es6 那樣的字符串模板文字。但是,您絕對可以使用fmt.Sprintf執行類似的操作。
fmt.Sprintf("hello?%s!?happy?coding.",?input)
在你的情況下,它將是:
bot := DigitalAssistant{
? ? fmt.Sprintf("%s", input),
? ? "teamAwesome",
? ? "awesomebotimagename",
? ? "0.1.0",
? ? 1,
? ? 8000,
? ? "health",
? ? "[email protected]",
}
順便說一句,這是一個好奇的問題。為什么需要在非常簡單的字符串(例如 )上使用字符串模板文字${input}?為什么不只是input?
編輯1
我不能只輸入輸入,因為 go 給出錯誤無法使用輸入(類型 []byte)作為字段值中的類型字符串
[]byte可轉換為字符串。如果您的input類型是[]byte,只需將其寫為string(input).
bot := DigitalAssistant{
? ? string(input),
? ? "teamAwesome",
? ? "awesomebotimagename",
? ? "0.1.0",
? ? 1,
? ? 8000,
? ? "health",
? ? "[email protected]",
}
編輯2
如果值是 int,為什么我不能做同樣的事情?因此,對于括號 1 和 8000 中的值,我不能只執行int(numberinput)or int(portinput),否則我會收到錯誤cannot use numberinput (type []byte) as the type int in field value
可以通過使用顯式轉換來實現從string到或從 到 的轉換。然而,這種方法并不適用于所有類型。[]byteT(v)
例如,要轉化[]byte為int更多的努力是需要的。
// convert `[]byte` into `string` using explicit conversion
valueInString := string(bytesData)?
// then use `strconv.Atoi()` to convert `string` into `int`
valueInInteger, _ := strconv.Atoi(valueInString)?
fmt.Println(valueInInteger)
- 1 回答
- 0 關注
- 139 瀏覽
添加回答
舉報