ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 第1.1语言基本语法 - 变量声明 var 变量名 数据类型,可以同时赋值,另一种方式只能在函数内部,变量名:=数据值,以及多变量声明 全局变量声明必须以 var 关键字开头,如果想要在外部包中使用全局变量的首字母必须大写,go语言区分大小写。 ``` <pre class="calibre14">``` var a int<span class="token">=</span><span class="token3">1</span> var <span class="token2">(</span> a int b string <span class="token2">)</span> b<span class="token2">:</span><span class="token">=</span><span class="token3">2.1</span> 只用于函数中 i<span class="token2">,</span>j<span class="token2">:</span><span class="token">=</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span> <span class="token6">//可用于交换</span> var a<span class="token">=</span><span class="token3">1</span>赋值 a <span class="token2">:</span><span class="token">=</span><span class="token3">1</span> 简短赋值 _ 表示匿名变量 ``` ``` - 整形int浮点型float int8 -128-127 int16 int32 int64 uint8 0-255 uint16 uint32 uint64 无符号整型 float32 float64 浮点型 var x complex128 = complex(1, 2) //表示64位复数 1+2i - bool 布尔型只有true,false - 字符串String \\n \\t \\r \\u表示Unicode \\表示本身 拼接字符 + 多行字符串反双引号 `` - 单字母 byte var ch byte = 65 表示A - 类型转换,必须显示转换 b:=int(b) ``` <pre class="calibre14">``` strconv<span class="token2">.</span><span class="token1">Itoa</span><span class="token2">(</span>int<span class="token2">)</span> 整型转换成字符串 strconv<span class="token2">.</span><span class="token1">Atoi</span><span class="token2">(</span>str<span class="token2">)</span> 字符串转换整型 ``` ``` - 指针类型,在变量前添加&,指针取值符号 \* - 常量 const pi=3.14233 值可以确定 -数组 ``` <pre class="calibre14">``` var q <span class="token2">[</span><span class="token3">3</span><span class="token2">]</span>int <span class="token">=</span> <span class="token2">[</span><span class="token3">3</span><span class="token2">]</span>int<span class="token2">{</span><span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">}</span> q <span class="token2">:</span><span class="token">=</span> <span class="token2">[</span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token2">]</span>int<span class="token2">{</span><span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">}</span> 自动确定 var arr <span class="token2">[</span><span class="token3">2</span><span class="token2">]</span>int 另一种方法 arr<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token">=</span><span class="token3">1</span> arr<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token">=</span><span class="token3">2</span> arr<span class="token2">:</span><span class="token">=</span> <span class="token2">[</span><span class="token3">3</span><span class="token2">]</span>string<span class="token2">{</span><span class="token4">"nihk"</span><span class="token2">,</span> <span class="token4">"你好吗"</span><span class="token2">,</span> <span class="token4">"测试11"</span><span class="token2">}</span> <span class="token5">for</span> k<span class="token2">,</span> v <span class="token2">:</span><span class="token">=</span> range arr <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span>k<span class="token2">,</span> v<span class="token2">)</span> <span class="token2">}</span> var array <span class="token2">[</span><span class="token3">4</span><span class="token2">]</span><span class="token2">[</span><span class="token3">2</span><span class="token2">]</span>int 多维数组<span class="token3">4</span>组每一组两个 ``` ``` - 切片 ``` <pre class="calibre14">``` var strList <span class="token2">[</span><span class="token2">]</span>string 字符串切片 var numListEmpty <span class="token">=</span> <span class="token2">[</span><span class="token2">]</span>int<span class="token2">{</span><span class="token2">}</span> 空切片 a <span class="token">=</span> <span class="token1">append</span><span class="token2">(</span>a<span class="token2">,</span> <span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">)</span> 切片追加元素 ``` ``` - map类型,映射,关键数组,字典,hash同一个意思 - list 列表