ZBLOG

golang any convert to string

Sure, in Go, you can convert any data type to a string using the fmt package. Here’s an example:

package main

import (
    "fmt"
)

func main() {
    // Convert an int to a string
    num := 123
    str := fmt.Sprintf("%d", num)
    fmt.Println(str) // Output: "123"

    // Convert a float to a string
    floatNum := 3.14
    str = fmt.Sprintf("%f", floatNum)
    fmt.Println(str) // Output: "3.140000"

    // Convert a boolean to a string
    boolVal := true
    str = fmt.Sprintf("%t", boolVal)
    fmt.Println(str) // Output: "true"
}

In this example, we use fmt.Sprintf() function to convert different types of values into strings. We pass the format specifier %d, %f, or %t depending on the type we want to convert, followed by the value itself.

I hope that helps! Let me know if you have any other questions.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=2949

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?