首页 > 其他分享 >go任意类型转字符串

go任意类型转字符串

时间:2024-02-15 22:01:05浏览次数:21  
标签:case Itoa int strconv value 字符串 key go 任意

直接上代码

package main

import (
    "encoding/json"
    "fmt"
    "strconv"
)

type Result struct {
    RequestID string `json:"request_id"`
    Code      string `json:"code"`
    Msg       string `json:"msg"`
}

func main() {
    var a Result
    a.RequestID = `123456789`
    a.Code = `999`
    a.Msg = `success`
    b := Strval(a)
    fmt.Println(b)

}

// Strval 获取变量的字符串值
// 浮点型 3.0将会转换成字符串3, "3"
// 非数值或字符类型的变量将会被转换成JSON格式字符串
func Strval(value interface{}) string {
    var key string
    if value == nil {
        return key
    }

    switch value := value.(type) {
    case float64:

        key = strconv.FormatFloat(value, 'f', -1, 64)
    case float32:

        key = strconv.FormatFloat(float64(value), 'f', -1, 64)
    case int:

        key = strconv.Itoa(value)
    case uint:

        key = strconv.Itoa(int(value))
    case int8:

        key = strconv.Itoa(int(value))
    case uint8:

        key = strconv.Itoa(int(value))
    case int16:

        key = strconv.Itoa(int(value))
    case uint16:

        key = strconv.Itoa(int(value))
    case int32:

        key = strconv.Itoa(int(value))
    case uint32:

        key = strconv.Itoa(int(value))
    case int64:

        key = strconv.FormatInt(value, 10)
    case uint64:

        key = strconv.FormatUint(value, 10)
    case string:
        key = value
    case []byte:
        key = string(value)
    default:
        newValue, _ := json.Marshal(value)
        key = string(newValue)
    }

    return key
}

 

标签:case,Itoa,int,strconv,value,字符串,key,go,任意
From: https://www.cnblogs.com/agfox123/p/18016667

相关文章

  • go的依赖管理
    1)为当前项目新增一个依赖包gomodtidy2)升(降)级版本 查看版本信息1.8有问题需要降级(升级也同理) 或者 ......
  • Go学习指南练习:映射
    题目:实现WordCount。它应当返回一个映射,其中包含字符串s中每个“单词”的个数。函数wc.Test会对此函数执行一系列测试用例,并输出成功还是失败。你会发现strings.Fields很有帮助。packagemainimport("golang.org/x/tour/wc""strings")funcWordCount(sstr......
  • Go - map
       ......
  • bug-missing GOSUMDB
     问题描述:D:\gopj>gomodtidygo:findingmoduleforpackagego.uber.org/zapgo:findingmoduleforpackagegithub.com/valyala/fasthttpgo:downloadinggo.uber.org/zapv1.26.0go:downloadinggithub.com/valyala/fasthttpv1.52.0go:githun.com/bigwh......
  • vscode中无法识别go命令
    现象描述:装好go在cmd下面输入goversion能正常显示但在vscode控制台中无法显示原因:vscode未识别到go命令解决方案:手动为vscode添加go的path路径 打开VSCode设置(JSON):你可以通过按下Ctrl+Shift+P打开命令面板,然后输入OpenSettings(JSON)并选择它来直......
  • 8小时速成golang(五)golang高阶 channel基本定义和使用
     1、定义channel变量channel是Go语言中的一个核心类型,可以把它看成管道。并发核心单元通过它就可以发送或者接收数据进行通讯,这在一定程度上又进一步降低了编程的难度。 channel是一个数据类型,主要用来解决go程的同步问题以及go程之间数据共享(数据传递)的问题。goroutin......
  • Django使用聚合查询(价格乘以总数得到总价,并以总价排名)
    自定义库存表(Stock)classStock(models.Model):amount=amount=models.IntegerField(verbose_name='数量')price=models.DecimalField(max_digits=10,decimal_places=2,verbose_name='单价')使用模板语法完成自定义查询:Stock.objects.annotate(profit=F(......
  • Go 100 mistakes - #26: Slices and memory leaks
        Asaruleofthumb,rememberthatslicingalargesliceorarraycanleadtopotential highmemoryconsumption.Theremainingspacewon’tbereclaimedbytheGC,and wecankeepalargebackingarraydespiteusingonlyafewelements.Using......
  • Go 100 mistakes - #25: Unexpected side effects using slice append
        ......
  • Go语言指南练习:切片
    题目:实现Pic。它应当返回一个长度为dy的切片,其中每个元素是一个长度为dx,元素类型为uint8的切片。当你运行此程序时,它会将每个整数解释为灰度值(好吧,其实是蓝度值)并显示它所对应的图像。图像的选择由你来定。几个有趣的函数包括(x+y)/2,x*y,x^y,x*log(y)和x%(y+1)。(提示:需要......