GO language basic commands, types and formats

basic command

Go language basic commands

go build * specifies to compile GO files, and we can execute them directly.

go run * specifies to compile and execute our GO

go fmt format code

go install * Compile and install a package

go get * Download a package

go test runs all test files of the current project

go version View the current go language version

go env GOROOT installation path

basic type

uint8 integer type 0-255 2 to the 8th power

uint16 integer type 0-65535 2 to the 16th power

uint32 integer type 0-4294967295 2 to the 32nd power

uint64 integer type 0- 64-bit integer type

basic grammar

package main

import "fmt"

func main() {
   //定义变量
   var a int
   var b string
   //输出数据类型和数据值  int %!V(int=0)
   fmt.Printf("%T %V \n", a, a)
   //直接打印我们定义的变量
   fmt.Println(a, b)

   //多变量定义
   var (
      c int
      f string
      d float32
      g []int
      l [3]int
      //也可以直接赋值
      e int32 = 200
   )
   //输出  0  0 [] 200 [0 0 0]
   fmt.Println(c, f, d, g, e, l)
}

different variable definitions 

package main
import "fmt"
//定义全局变量
var s = "njsnjdnsj"
func main() {
	//不指定类型定义
	var (
		c = 50
		a = 50.665
	)
	//也可以这么定义
	d := 25
	//输出  int %!V(int=50)
	fmt.Printf("%T %V \n", c, c)
	//输出  float64 %!V(float64=50.665)
	fmt.Printf("%T %V \n", a, a)
	//输出  int %!V(int=25)
	fmt.Printf("%T %V \n", d, d)
}

Multivariate Definitions and Anonymous Variables

package main

import "fmt"

func main() {

	//多变量定义
	d, e, s := 25, "1222", "dsad"
	//定义匿名变量   可以使用 _ 来代替,匿名变量不会占用空间和分配内存
	f, _, _ := 25, "1222", "dsad"
	fmt.Printf("%T %V \n", d, d)
	fmt.Printf("%T %V \n", e, e)
	fmt.Printf("%T %V \n", s, s)
	fmt.Printf("%T %V \n", f, f)
}

multiple assignment

func main() {
	//多重赋值
	d := 20
	c := 30
	//输出 20 30
	fmt.Println(d, c)
	c, d = d, c
	//输出 30 20
	fmt.Println(d, c)
}

byte and rune and ASCLL

func main() {
	//byte 和 rune其实就是int别名
	var a byte = 100
	var b rune = 200
	//byte带包ASCLL码字符   uint8的别名
	var e byte = 'G'
	//rune带包ASCLL码字符   uint32的别名
	var f rune = 'G'
	var g int = 97
	//uint8 , %!V(uint8=100)
	fmt.Printf("%T , %V \n", a, a)
	//int32 , %!V(int32=200)
	fmt.Printf("%T , %V \n", b, b)
	fmt.Printf("%T , %V \n", e, e)
	fmt.Printf("%T , %c \n", f, f)
	//%c 反向打印ASCLL码
	fmt.Printf("%T , %c \n", g, g)
}

common format

func main() {
   var a int = 100
   str := "Hello"
   //%v represents the value of the output data in the default format Hello
   fmt.Printf("%v \n", str)
   //Indicates the output data type and object type int
   fmt.Printf("%T \n", a)
   //for Boolean type true
   fmt.Printf("%t \n", true)
   fmt.Printf("%c \n", a)
   //Output integer, otherwise it will output %!d(type=value) 100
   fmt.Printf("%d \n", a)
   // output binary array
   fmt.Printf("%b \n", a)
   //Indicates that it must be 8 bits, if it is insufficient, add 0 in front 00000100
   fmt.Printf("%08d \n", a)
   //Indicates that it must be 8 digits, if it is insufficient, fill in a space of 100 in front
   fmt.Printf("%8d \n", a)
   //Assign the result Keyword: Sprintln
   str = fmt.Sprintln("%8d \n", a)
   fmt.Println(str)
}

Float and string output 

func main() {
	//浮点型  23.500000       相当于%f6
	fmt.Printf("%f \n", 23.5)
	//浮点型  23.52           四舍五入切割保留2位
	fmt.Printf("%.2f \n", 23.5222)
	//字符串   区块链技术      没有双引号
	fmt.Printf("%s \n", "区块链技术")
	//字符串	  "区块链技术"    有双引号
	fmt.Printf("%q \n", "区块链技术")
	//         'Ǵ'
	fmt.Printf("%q \n", 500)
	//对数组进行输出         ABC
	arr := []byte{'A', 'B', 'C'}
	fmt.Printf("%s \n", arr)
}

おすすめ

転載: blog.csdn.net/qq_38935605/article/details/124338555