Introduction à la conversion entre le type de données de base Go et le type de chaîne

 

Cet article présente principalement la conversion mutuelle entre les types de données de base Go et les types de chaîne. L'article présente l'exemple de code en détail. Il a une certaine valeur d'apprentissage de référence pour l'étude ou le travail de chacun. Amis qui en ont besoin, suivez l'éditeur pour apprendre ensemble .

1. Type de données de base en type chaîne

Méthode 1 : fmt.Sprintf("% paramètre", expression)

1) Explication officielle : Sprintf génère une chaîne formatée selon le paramètre format et renvoie la chaîne.

1

func Sprintf(format string, a ...interface{}) string

2) Méthode d'utilisation spécifique :

① Type de nombre à type de chaîne

1

2

var num1 int = 99

str := fmt.Sprintf("%d", num1)

② Type flottant en type chaîne

1

2

var num2 float64 = 23.456

str := fmt.Sprintf("%f", num2)

③ Type booléen en type chaîne

1

2

var b bool = true

str := fmt.Sprintf("%t", b)

④ Type de caractère (octet) vers type de chaîne

1

2

var mychar byte = 'h'

str := fmt.Sprintf("%c", mychar)

Méthode 2 : Utiliser les fonctions du package strconv

1

2

3

4

5

func FormatBool(b bool) string

func FormatInt(i int64, base int) string

func FormatUint(i uint64, base int) string

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

func Itoa(i int) string

① Type de nombre à type de chaîne (deux façons)

1

2

3

4

5

var num1 int = 99

//第一个参数需转化为int64类型,第二个参数表示几进制

str := strconv.FormatInt(int64(num1), 10)

var num1 int = 99

str := strconv.Itoa(num1)

② Type flottant en type chaîne

1

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

f représente le nombre à virgule flottante à convertir

format de représentation fmt : 'f' (-ddd.dddd), 'b' (-ddddp±ddd, l'exposant est binaire), 'e' (-d.dddde±dd, exposant décimal), 'E' (-d. ddddE±dd, exposant décimal), 'g' (utiliser le format 'e' pour les grands exposants, sinon le format 'f'), 'G' (utiliser le format 'E' pour les grands exposants, sinon le format 'f').

prec contrôle la précision (hors partie exposant) : pour 'f', 'e', ​​​​'E', il indique le nombre de chiffres après la virgule ; pour 'g', 'G', il contrôle le total nombre de chiffres. Si prec vaut -1, cela signifie qu'il faut utiliser le nombre minimum de nombres requis pour représenter f.

bitSize indique le type de source de f (32 : float32, 64 : float64), et il sera arrondi en conséquence.

1

2

var num2 float64 = 23.456

str := strconv.FormatFloat(num2, 'f', 3, 64)

③ Type booléen en type chaîne

1

2

var b bool = true

str := strconv.FormatBool(b)

2. Type de chaîne au type de données de base

Utilisation des fonctions du package strconv

① Type de chaîne en type booléen

1

2

var str string = "true"

b, _ := strconv.ParseBool(str)

② Type de chaîne vers type de nombre (deux façons)

un.

1

func ParseInt(s string, base int, bitSize int) (i int64, err error)

Renvoie la valeur entière représentée par la chaîne, en acceptant un signe.

base指定进制(2到36),如果base为0,则会从字符串前置判断,"0x"是16进制,"0"是8进制,否则是10进制;

bitSize指定结果必须能无溢出赋值的整数类型,0、8、16、32、64 分别代表 int、int8、int16、int32、int64;返回的err是*NumErr类型的,如果语法有误,err.Error = ErrSyntax;如果结果超出类型范围err.Error = ErrRange。

1

2

var str string = "1234"

n, _ := strconv.ParseInt(str, 0, 64)

b.

1

2

var str string = "1234"

num, _ := strconv.Atoi(str)

③字符串类型转浮点类型

1

func ParseFloat(s string, bitSize int) (f float64, err error)

如果s合乎语法规则,函数会返回最为接近s表示值的一个浮点数(使用IEEE754规范舍入)。bitSize指定了期望的接收类型,32是float32(返回值可以不改变精确值的赋值给float32),64是float64;返回值err是*NumErr类型的,语法有误的,err.Error=ErrSyntax;结果超出表示范围的,返回值f为±Inf,err.Error= ErrRange。

1

2

var str string = "123.456"

n, _ := strconv.ParseFloat(str, 64)

到此这篇关于Go基本数据类型与string类型互转的文章就介绍到这了,希望可以帮到你

转自:微点阅读   https://www.weidianyuedu.com

Je suppose que tu aimes

Origine blog.csdn.net/weixin_45707610/article/details/131829621
conseillé
Classement