Maison  >  Article  >  développement back-end  >  Un article pour vous aider à comprendre les types de données de base du langage Go

Un article pour vous aider à comprendre les types de données de base du langage Go

Go语言进阶学习
Go语言进阶学习avant
2023-07-20 15:47:31550parcourir

iota

是Go中的一个常量计数器,只能在跟常量(const)一块使用。

我们先来理解这一段话。

iota在const关键字出现时将被重置为0const中每新增一行常量,iota将计数(+1)一次

示例1:

package main

import "fmt"

func main() {
    const (
        n1 = iota //在const关键字出现时将被重置为0
        n2        //没写相当于写了个n2=iota,每新增一行常量iota计数(+1)一次,n2 = 1
        n3        //同上 n3 = 2
        n4        // 同上 n4 = 3
)
    fmt.Println(n1, n2, n3, n4)
}

示例2:

遇到_会是什么情况。

package main

import "fmt"

func main() {
    const (
        n1 = iota //在const关键字出现时将被重置为0
        n2        // n2=1
        _         //匿名变量,相当于写了个 _=iota,所以此时iota=2
        n4        //n4=3
)
    fmt.Println(n1, n2, n4)
}

示例3:

一行多个iota est un Constante

compteur dans Go, qui ne peut être utilisé qu'avec une constante (const). 🎜🎜

🎜Commençons par comprendre ce paragraphe. 🎜🎜

package main

import "fmt"

func main() {
    const (
        a, b = iota + 1, iota + 2 //在const关键字出现时,iota=0,并且两次赋值在同一行,iota没有做+1
        c, d                      //同理,新增一行常量,常量个数为俩,仍然是一行,所以iota=1
        //c, d = iota + 1, iota + 2 同上,此时iota=1,c=2,d=3
        e, f // 同理,同上,e=3,f=4
)
    fmt.Println(a, b, c, d, e, f)
}

🎜Exemple 1 :🎜🎜

package main

import "fmt"

func main() {
    var a = 10             //定义一个十进制数
    fmt.Printf("%T\n", a)  //功能:打印变量类型,结果:默认为int类型,也叫int64
    fmt.Printf("%d \n", a) //功能:十进制输出,结果:10
    fmt.Printf("%b \n", a) //功能:二进制输出,结果:1010
    fmt.Printf("%o \n", a) //功能:二进制输出,结果:12

    var b = 0b1010011010  //定义一个二进制数1010011010,以0b开头
    fmt.Printf("%d\n", b) //结果:666

    var c = 077           //定义一个八进制数77
    fmt.Printf("%d\n", c) //结果:63
    var d = 0x42          //定义一个十六进制42
    fmt.Printf("%d\n", d) //结果:66
}
🎜

🎜Exemple 2 :🎜
🎜

package main

import "fmt"

func main() {
    var a = 1.21          //默认为float64
    fmt.Printf("%T\n", a) //结果:float64
    fmt.Printf("%f\n",a)//功能:输出浮点型数,结果:1.210000
    fmt.Printf("%.1f\n",a)//功能:输出浮点型数,保留小数点一位,其他忽略,结果:1.2
    fmt.Printf("%.2f\n",a)//功能:输出浮点型数,保留小数点二位,其他忽略,结果:1.21
}
🎜

🎜Exemple 3 :🎜
🎜

🎜Multiple sur une seule ligne🎜🎜iota code >🎜🎜. 🎜🎜<section class="code-snippet__fix code-snippet__js"><pre class="brush:php;toolbar:false;">package main import &quot;fmt&quot; func main() { const ( a, b = iota + 1, iota + 2 //在const关键字出现时,iota=0,并且两次赋值在同一行,iota没有做+1 c, d //同理,新增一行常量,常量个数为俩,仍然是一行,所以iota=1 //c, d = iota + 1, iota + 2 同上,此时iota=1,c=2,d=3 e, f // 同理,同上,e=3,f=4 ) fmt.Println(a, b, c, d, e, f) }</pre></section><h2 cid="n139" mdtype="heading"><br></h2> <h2 cid="n139" mdtype="heading" style='break-after: avoid-page;break-inside: avoid;orphans: 4;font-size: 1.75em;margin-top: 1rem;margin-bottom: 1rem;font-weight: bold;line-height: 1.225;cursor: text;border-bottom: 1px solid rgb(238, 238, 238);white-space: pre-wrap;font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;text-align: start;'> <span md-inline="plain">整型</span><br> </h2> <p cid="n140" mdtype="paragraph" style='line-height: inherit;orphans: 4;margin-top: 0.8em;margin-bottom: 0.8em;white-space: pre-wrap;font-family: "Open Sans", "Clear Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;font-size: 16px;text-align: start;'><span md-inline="plain">整数,很简单了,就是像</span><span md-inline="code" spellcheck="false"><code style="font-family: var(--monospace);vertical-align: initial;border-width: 1px;border-style: solid;border-color: rgb(231, 234, 237);background-color: rgb(243, 244, 244);border-radius: 3px;padding-right: 2px;padding-left: 2px;font-size: 0.9em;">1,2,3,7,11,..这样的整型数字了。

但是在Go中,整数分为两大类,正整数和没有符号的整数。

u开头的不能存负数

uint8
uint32🎜🎜🎜有符号 8位整型 (- 128 到 127)🎜🎜🎜🎜<tr cid="n162" mdtype="table_row" style="box-sizing : border-box;break-inside : éviter ;break-after : auto;border-top : 1px solid rgb (223, 226, 229);couleur d'arrière-plan : rgb(248, 248, 248);">🎜<span cid="n163" mdtype="table_cell" style="box-sizing : border-box; affichage : en ligne -block;min-width: 1ch;width: 82.6667px;min-height: 10px;">🎜<code style="box-sizing: border-box;font-family: var(--monospace);text-align : gauche ; alignement vertical : initial ; largeur de bordure : 1 px ; style de bordure : solide ; couleur de bordure : RVB (231, 234, 237); couleur d'arrière-plan : RVB (243, 244, 244); rayon de bordure : 3px;padding-right: 2px;padding-left: 2px;font-size: 0.9em;">int16🎜🎜🎜🎜 🎜🎜
uint16 无符号 16位整型 (0 到 65535)
无符号 32位整型 (0 到 4294967295)uint8 无符号 8位整型 (0 到 255)
uint16 无符号 16位整型 (0 到 65535)
uint32
uint64uint64 无符号 64位整型 (0 到 18446744073709551615)
int8 有符号 8位整型 (-128 到 127)
int16 有符号 16位整型 (-32768 到 32767)
int32 无符号 64位整型 (0 到 18446744073709551615)
int64 Entier 64 bits signé (-9223372036854775808 à 9223372036854775807)

Différences sur différentes plateformes.

uint
uint64int64uintptr
<span md-inline="code" spellcheck="false" style="box-sizing: border-box;"><code style="box-sizing: border-box;font-family: var(--monospace);text-align: left;vertical-align: initial;border-width: 1px;border-style: solid;border -couleur : RVB (231, 234, 237) ; couleur d'arrière-plan : RVB (243, 244, 244) ; rayon de bordure : 3 px ; remplissage à droite : 2 px ; remplissage à gauche : 2 px ; ">int32int32,64位操作系统上就是int64
uintptr,64位操作系统上就是
🎜🎜🎜无符号整型,用于存放一个指针🎜🎜🎜🎜🎜🎜

示例:

package main

import "fmt"

func main() {
    var a = 10             //定义一个十进制数
    fmt.Printf("%T\n", a)  //功能:打印变量类型,结果:默认为int类型,也叫int64
    fmt.Printf("%d \n", a) //功能:十进制输出,结果:10
    fmt.Printf("%b \n", a) //功能:二进制输出,结果:1010
    fmt.Printf("%o \n", a) //功能:二进制输出,结果:12

    var b = 0b1010011010  //定义一个二进制数1010011010,以0b开头
    fmt.Printf("%d\n", b) //结果:666

    var c = 077           //定义一个八进制数77
    fmt.Printf("%d\n", c) //结果:63
    var d = 0x42          //定义一个十六进制42
    fmt.Printf("%d\n", d) //结果:66
}


浮点型

在Go中,只有float32float64,默认使用的是float64

示例:

package main

import "fmt"

func main() {
    var a = 1.21          //默认为float64
    fmt.Printf("%T\n", a) //结果:float64
    fmt.Printf("%f\n",a)//功能:输出浮点型数,结果:1.210000
    fmt.Printf("%.1f\n",a)//功能:输出浮点型数,保留小数点一位,其他忽略,结果:1.2
    fmt.Printf("%.2f\n",a)//功能:输出浮点型数,保留小数点二位,其他忽略,结果:1.21
}


布尔值

在Go中,bool类型就俩值,truefalse

无示例。

注:

  1. bool类型默认值为false

  2. true != 1,在Go中,bool类型和整型不能混用。


字符串

终于到字符串了,在Go中,字符串是基本数据类型,在栈中存储。

字符串的值为双引号("")中的内容。

示例:

package main

import "fmt"

func main() {
    var name = "hello"
    var name2 = "张三"
    fmt.Println(name,name2)
}

在Go中,字符串内存布局如下。

Un article pour vous aider à comprendre les types de données de base du langage Go

En fait, dans Go, l'essence d'une chaîne est d'assembler les caractères un par un.


Chaîne multi-lignes

Parfois, nous pouvons avoir besoin d'un long article et d'une longue chaîne, nous devons alors utiliser le numéro `.

package main

import "fmt"

func main() {
    var lyric = `
        昨夜同门云集bai 推杯又换盏
        今朝du茶凉酒寒 豪言成笑谈
        半生累 尽徒然zhi 碑文完美有谁看dao
        隐居山水之间 誓与浮名散
        湖畔青石板上 一把油纸伞
    `
    fmt.Println(lyric)
}


字符串常用操作
len(str)

求长度+或fmt.Sprintfstrings.Splitstrings.HasPrefix,strings.HasSuffix
分割len(str) 求长度
+或fmt.Sprintf 拼接字符串
strings.Split
strings.containsstrings.contains 判断是否包含
strings.HasPrefix,strings.HasSuffix 前缀/后缀判断
strings.Index(),strings.LastIndex() 子串出现的位置
strings.Join(a[]string, sep string) 判断是否包含
🎜🎜🎜前缀/后缀判断🎜🎜🎜🎜🎜🎜strings.Index(),strings.LastIndex()🎜🎜🎜🎜🎜子串出现的位置🎜🎜🎜🎜🎜🎜数据类型 String 常量 整型 浮点型 const 字符串 bool 指针 Go语言 iota float32 len
Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer