Welcome to my blog and github!

Go language learning notes first play, from Gotour, in the future to often write notes, write down their own study notes, even if only notes to write more.

Bad writing is better than a good memory, but also to exercise their writing ability.

To tell the truth, I am very tired today. I have been working on the operating system kernel recently, because I wrote a bootloader originally, and now I want to switch to GRUB, but I have encountered too many pits for two days 😭.

Or contact a little new knowledge simple things, to buffer, the brain is foggy.

package

Each Go program consists of many packages.

Programs are run from the main package.

The program is using packages with import paths “FMT” and “Math/RAND”.

By convention, the package name is the same as the last element of the import path.

For example, the “Math/RAND” package includes files that begin with the statement package RAND.

import

This code groups imports into parenthesized “decomposed” import statements.

You can also write multiple import statements, such as:

package main

import (
	"fmt"
	"math"
)

func main(a) {
	fmt.Println(math.Pi)
}

Copy the code

But using a shred import statement is a good style.

Export name

In Go, if the name begins with a capital letter, the name is exported.

For example, Pizza is an exported name, as is Pi, which is exported from the Math package.

Pizza and PI do not begin with a capital letter, so they are not exported.

When importing a package, you can reference only the names it exports. Any “unexported” names are not accessible from outside the package.

package main

import (
	"fmt"
	"math"
)

func main(a) {
	fmt.Println(math.Pi)
}

Copy the code

function

A function can take zero or more arguments.

In this example, add takes two arguments of type int.

Note that the type comes after the variable name.

package main

import "fmt"

func add(x int, y int) int {
	return x + y
}

func mins(x int, y int) int {
	return x - y;
}

func main(a) {
	fmt.Println(add(42.13))
	fmt.Println(mins(23.11))}Copy the code

When two or more consecutive named function parameters share a type, the type can be omitted from all but the last parameter.

In this case, we shortened it

x int, yint
Copy the code

to

The x, y,int

Copy the code
package main

import "fmt"

func add(x, y int) int {
	return x + y
}

func main(a) {
	fmt.Println(add(42.13))}Copy the code

Multiple return values

A function can return multiple return values.

package main

import "fmt"

func swap(a, b string) (string.string) {
	return b, a
}

func main(a) {
	a, b := swap("ai"."ni")
	fmt.Println(a, b)
}

Copy the code

Named return values

The return value of Go may be named.

If so, they are treated as variables defined at the top of the function.

You should use these names to record the meaning of the return value.

A return statement without arguments returns the specified return value.

This is called a “naked” return.

Bare return statements should only be used in short functions, as shown in the following example.

In longer functions, they can impair readability.

package main

import "fmt"

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


func main(a) {
	fmt.Println(split(17))}Copy the code

variable

The var statement declares a list of variables; Type is last in the function argument list.

Var statements can be either package-level or function-level. In this case we can see both.

package main

import "fmt"

var c, python, java bool

func main(a) {
	var i int
	fmt.Println(i, c, python, java)
}

Copy the code

Initialization value

Var declarations can contain initializers, one for each variable.

If there is an initialization, the type can be omitted; The variable will take the type of the initializer.

package main

import "fmt"

var i, j int = 1.2

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

Copy the code

Short variable initialization

Inside a function, you can use the := short assignment statement instead of a var declaration with an implicit type.

Outside of functions, each statement begins with a keyword (var, func, etc.), so the := structure is not available

package main

import "fmt"

func main(a) {
	var i, j int = 1.2
	k := 3
	c, python, java := true.false."no!"
	fmt.Println(a ,i, j, k, c, python, java)
}

Copy the code

Basic data types

Basic data types of the GO language

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 point

float32 float64

complex64 complex128
Copy the code

The example shows several types of variables, and like import statements, variable declarations can be “shred” into blocks.

The int, uint and Uintptr types are typically 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.

When you need integer values, you should use int unless there is a special reason to use size or unsigned integer types.

package main

import (
	"fmt"
	"math/cmplx"
)

var (
	ToBe   bool       = false
	MaxInt uint64     = 1<<64 - 1
	z      complex128 = cmplx.Sqrt(- 5 + 12i))func main(a) {
	fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
	fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
	fmt.Printf("Type: %T Value: %v\n", z, z)
}

Copy the code

Zero value

Variables that are declared without an explicit initial value are assigned a zero value.

Zero value is:

The number type is 0.

False for booleans

“(empty string) represents a string.

package main

import "fmt"

func main(a) {
	var i int
	var f float64
	var b bool
	var s string
	fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

Copy the code

Type conversion

Convert the v value to type T using T(v)

Some examples:

package main

import "fmt"

func main(a) {
	v := 12.2 // change me!
	fmt.Printf("v is of type %T\n", v)
}

Copy the code

Unlike C, the allocation between different types of items in Go requires explicit conversions.

Type inference

When you declare a variable without specifying an explicit type (using: = syntax or var = expression syntax), the type of the variable is inferred from the value on the right.

When you type the right side of the declaration, the new variable has the same type:

var i intJ: = I// j is an integer
Copy the code

However, when the right hand side contains an untyped numeric constant, the new variable may be int, FLOAT64, or complex128, depending on the precision of the constant:

I: =42 / / integerF: =3.142 // 

float64G: =0.867 + 0.5 I //complex128
Copy the code
package main

import "fmt"

func main(a) {
	v := 12.2 // change me!
	fmt.Printf("v is of type %T\n", v)
}

Copy the code

constant

Constants are declared like variables, but with the const keyword.

Constants can be characters, strings, Booleans, or numeric values.

Constants cannot be declared using the: = syntax.

package main

import "fmt"

const Pi = 3.14

func main(a) {
	const World = "The world"
	fmt.Println("Hello", World)
	fmt.Println("Happy", Pi, "Day")

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

Copy the code

Digital constant

Numerical constants are high precision values.

Untyped constants take the type required by their context.

(An int can store up to one 64-bit integer, sometimes less.)

package main

import "fmt"

const (
	// 1 moves 100 bits to the left
	Big = 1 << 100
	// Move 99 to the right
	Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
	return x * 0.1
}

func main(a) {
	fmt.Println(needInt(Small))
	fmt.Println(needFloat(Small))
	fmt.Println(needFloat(Big))
}

Copy the code

Welcome to my blog and github!