GO Language Learning II (Basic)

One, package

Every Go program is composed of packages.

The entry point for the program to run is the package main 

2. Import

Two ways:

multiple imports 

import "fmt"
import "math"

Group imports with parentheses, package imports

import (
	"fmt"
	"math"
)


3. Function

Functions can have no parameters or accept multiple parameters

When two or more consecutive function named parameters are of the same type, all but the last type can be omitted

Functions can return any number of return values

Go's return values ​​can be named and used like variables declared at the beginning of the function body

Example 1:

package main

import "fmt"

func swap(x, y string) (string, string) {
	return y, x
}

func main() {
	a, b := swap("hello", "world")
	fmt.Println(a, b)
}
Example two:

package main

import "fmt"

func split (sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x
	return
}

func main() {
	fmt.Println(split(17))
}

4. Variables

var The statement defines a list of variables; like a function's parameter list, the type follows

var Statements can be defined at the package or function level

Variable definitions can contain initial values, one for each variable.

If the initialization is using an expression, the type can be omitted; the variable gets the type from the initial value.

Short declaration of variables: In functions,  concise assignment statements can be used instead  of definitions := where they are clearly typed var


 := Structs cannot be used outside functions

Example 1:

package main

import "fmt"

var i, j int = 1, 2

func main() {
	var c, python, java = true, false, "no!"
	fmt.Println(i, j, c, python, java)
}

Example two:

package main

import "fmt"

func main() {
	var i, j int = 1, 2
	k := 3
	c, python, java := true, false, "no!"

	fmt.Println (i, j, k, c, python, java)
}

Five, the basic type

Go's basic types are Basic types

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code

float32 float64

complex64 complex128

Example 1:

package main

import (
	"fmt"
	"math/cmplx"
)

where (
	ToBe   bool       = false
	MaxInt uint64 = 1 << 64 - 1
	z      complex128 = cmplx.Sqrt(-5 + 12i)
)


func main() {
	const f = "%T(%v)\n"
	fmt.Printf (f, ToBe, ToBe)
	fmt.Printf (f, MaxInt, MaxInt)
	fmt.Printf(f, z, z)
}

Zero value:

  • The numeric type is  0 ,
  • The boolean type is  false ,
  • String is  "" (empty string).

type conversion

Expression  T(v) converts value  v to type T

var i int = 42
var f float64 = float64 (i)
var u uint = uint(f)


六、常量

常量的定义与变量类似,只不过使用 const 关键字

常量可以是字符、字符串、布尔或数字类型的值

常量不能使用 := 语法定义

样例一:

package main

import "fmt"

const Pi = 3.14

func main() {
	const World = "世界"
	fmt.Println("Hello", World)
	fmt.Println("Happy", Pi, "Day")

	const Truth = true
	fmt.Println("Go rules?", Truth)
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325731114&siteId=291194637