惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
罗磊的独立博客
The GitHub Blog
The GitHub Blog
L
LangChain Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
宝玉的分享
宝玉的分享
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
N
Netflix TechBlog - Medium
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
美团技术团队
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周
golang数据类型转换汇总
二丫讲梵 · 2022-07-17 · via 二丫讲梵

golang 作为强类型语言,我们在日常开发过程中,遇到类型转换的场景也非常多,本文整理我在工作开发中遇到过的类型转换实践。

# String To Other

  • string to int

    • 方法一

      	str := "10"
      	int, err := strconv.Atoi(str)
      

      1
      2

    • 方法二

      	str := "10"
      	int, err := strconv.ParseInt(str, 10, 0)
      

      1
      2

  • string to int8

    	str := "10"
    	int, err := strconv.ParseInt(str, 10, 8)
    

    1
    2

  • string to int32

    int32 也叫做 rune

    	str := "10"
    	int, err := strconv.ParseInt(str, 10, 32)
    

    1
    2

  • string to int64

    	str := "10"
    	int, err := strconv.ParseInt(str, 10, 64)
    

    1
    2

  • string to float32

    	ret, _ := strconv.ParseFloat("3.14159", 32)
    	fmt.Println(ret)
    

    1
    2

  • string to float64

    	ret, _ := strconv.ParseFloat("3.14159", 64)
    	fmt.Println(ret)
    

    1
    2

  • string to []byte

    	s1 := "hello"
      b := []byte(s1)
    

    1
    2

  • json string to map

    func JsonToMap(jsonStr string) (m map[string]string, err error) {
    	err = json.Unmarshal([]byte(jsonStr), &m)
    	if err != nil {
    		return nil, err
    	}
    	return
    }
    

    1
    2
    3
    4
    5
    6
    7

# Other To String

  • []byte to string

    	var s1 []byte
    	fmt.Println(string(s1))
    

    1
    2

  • int64 to string

    int, int32, int16, int8 转十进制字符串

    	var number int = 12 // 您可以在这里使用任何整数: int32, int16, int8
    	s := strconv.FormatInt(int64(123), 10)
    	fmt.Println(s)
    

    1
    2
    3

    要转换intstring也可以使用strconv.Itoa (opens new window)which 等价于strconv.FormatInt(int64(i), 10) (opens new window).

    	number := 12
    	str := strconv.Itoa(number)
    	fmt.Println(str)
    

    1
    2
    3

# Map To Other

  • map to json string

    func MapToJson(m map[string]string) (string, error) {
    	result, err := json.Marshal(m)
    	if err != nil {
    		return "", nil
    	}
    	return string(result), nil
    }
    

    1
    2
    3
    4
    5
    6
    7

    通常也会使用一些第三方的 json 库进行解析,这里推荐:github.com/json-iterator/go (opens new window)

    package main
    
    import (
    	"fmt"
    
    	jsoniter "github.com/json-iterator/go"
    )
    
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    
    func main() {
    	tmpData := map[string]interface{}{"name": "eryajf", "age": 18}
    
    	output, err := json.Marshal(&tmpData)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(string(output))
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

  • map to slice

    func MapToSlice(m map[int]string) []string {
      s := make([]string, 0, len(m))
      for _, v := range m {
        s = append(s, v)
      }
      return s
    }
    

    1
    2
    3
    4
    5
    6
    7

# Json To Other

  • json to map

    func JsonToMap() {
    	jsonStr := `{"name": "eryajf","age": 18}`
    	var mapResult map[string]interface{}
    	err := json.Unmarshal([]byte(jsonStr), &mapResult)
    	if err != nil {
    		fmt.Println("JsonToMap err: ", err)
    	}
    	fmt.Println(mapResult)
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9

  • json to struct

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type People struct {
    	Name string `json:"name"`
    	Age int `json:"age"`
    }
    
    func main() {
    	jsonStr := `{"name": "eryajf","age": 18}`
    	var people People
    	json.Unmarshal([]byte(jsonStr), &people)
    	fmt.Println(people.Name, people.Age)
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    使用第三方 JSON 库进行解析:

    package main
    
    import (
    	"fmt"
    
    	jsoniter "github.com/json-iterator/go"
    )
    
    type People struct {
    	Name string `json:"name"`
    	Age int `json:"age"`
    }
    
    func main() {
    	jsonStr := `{"name": "eryajf","age": 18}`
    	var people People
    	json.Unmarshal([]byte(jsonStr), &people)
    	fmt.Println(people.Name, people.Age)
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

# Struct To Other

  • struct to json

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type People struct {
    	Name string `json:"name"`
    	Age int `json:"age"`
    }
    
    func main() {
    	p := People{
    		Name: "eryajf",
    		Age:  18,
    	}
    	jsonBytes, err := json.Marshal(p)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(string(jsonBytes))
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    同理,这里仍旧可以借助第三方 JSON 库进行解析:

    package main
    
    import (
    	"fmt"
    
    	jsoniter "github.com/json-iterator/go"
    )
    
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    
    type People struct {
    	Name string `json:"name"`
    	Age  int    `json:"age"`
    }
    
    func main() {
    	p := People{
    		Name: "eryajf",
    		Age:  18,
    	}
    	jsonBytes, err := json.Marshal(p)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(string(jsonBytes))
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26

未完。。。