This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Go small | basic grammar knowledge

Variable definitions

Use the var keyword

  • var a,b,c bool
  • var s1,s2 string = “hello”, “world”
  • You can put them inside functions or directly inside packages
  • Use var() to define variables centrally

Compiler type inference

  • var a, b, i, s1, s2 = true, false, 3, “hello”, “world”

Define variables using :=

  • a, b, i, s1, s2 := true, false, 3, “hello”, “world”
  • Can only be used within functions
package main

import "fmt"

// Package global cannot use := to define variables
var (
	aa = 3
	ss = "kkk"
	bb = true
)
// The variable is null
func variableZeroValue (a) {
	var a int
	var s string
	fmt.Printf("%d %q\n",a,s)
}
// Variable assignment
func variableInitialValue (a) {
	var a, b int = 3.4
	var s string = "abc"
	fmt.Println(a,b,s)
}
// Type inference
func variableTypeDeduction (a) {
	var a,b,c,s = 3.4.true."def"
	fmt.Println(a,b,c,s )
}
// := define variables
func variableShorter (a) {
	a,b,c,s := 3.4.true."def"
	b = 5 / / change the value
	fmt.Println(a,b,c,s )
}
func main(a){
	fmt.Println("Hello GoLang")
	variableZeroValue()
	variableInitialValue()
	variableTypeDeduction()
	variableShorter()
	fmt.Println(aa,ss,bb)
}
Copy the code
Hello GoLang
0 ""
3 4 abc
3 4 true def
3 5 true def
3 kkk true
Copy the code

Built-in variable types

  • bool, string4
  • (u)int, (u)int8,(u)int16,(u)int32,(u)int64,unitptr(pointer) u unsigned
  • Byte,rune (32-bit go language character, similar to char)
  • float32,float64,complex64,complex128
package main

import (
	"fmt"
	"math/cmplx"
)
func euler(a) {
	c := 3 + 4i
	fmt.Println(cmplx.Abs(c))
}
func main(a){
	fmt.Println("Hello GoLang")
	euler()
}
Copy the code
Hello GoLang
5
Copy the code

Cast casting

Casting is mandatory, and the go language casts after

package main

import (
	"fmt"
	"math"
)

func triangle (a) {
	var a,b int = 3.4
	var c int 
	c = int(math.Sqrt(float64(a*a + b*b))) // Cast
	fmt.Println(c)
}
func main(a){
	fmt.Println("Hello GoLang")
	triangle()
}
Copy the code

Definition of constant

Basic usage

package main

import (
	"fmt"
	"math"
)

func consts(a) {
	const filename = "abc.txt"
	const a,b = 3.4
	var c int
	c = int(math.Sqrt(a*a+b*b))
	fmt.Println(filename,c)
}

func main(a){
	consts()
}
Copy the code
abc.txt 5
Copy the code

Enumerated type

package main

import (
	"fmt"
)

func enums (a) {
	const(
		cpp = 0
		java = 1
		python = 2
		golang = 3
	)
	fmt.Println(cpp, java, python, golang)
}

func main(a){
	enums() // 0 1 2 3
}
Copy the code

iotaSince the appreciation

Auto-value-added enumeration type

package main

import (
	"fmt"
)

func enums (a) {
	const(
		cpp = iota / / the value
		_
		java 
		python 
		golang 
	)
	fmt.Println(cpp, java, python, golang)
}

func main(a){
	enums() // 0 2 3 4
}
Copy the code
/ / define b, KB, MB, gb, TB, pb
package main

import (
	"fmt"
)

func enums (a) {
	const(
		b = 1< < (10*iota)
		kb
		mb
		gb 
		tb
		pb
	)
	fmt.Println(b,kb,mb,gb ,tb,pb)
}

func main(a){
	enums() // 1 1024 1048576 1073741824 1099511627776 1125899906842624
}
Copy the code