Variables and basic data types in Go language

Time:2022-8-15

1. Basic introduction

Go is a static (compiled) language, a weakly typed language different from interpreted languages ​​(static: fixed type, strong type: different types do not allow direct operations)

For example, python is a dynamically typed language

1. Features of Go:

Cross-platform compiled language, cross-compilation

Pipe (channel), slice (slice), concurrent (routine)

Has garbage collection mechanism

Supports object-oriented and procedural programming patterns (Go's object-oriented has no concept of classes)

2. Common commands of Go:

go env // environment variables for go
    -GO111MODULE= // empty, not using MODULE mode now
    -GOPATH=C:\Users\YS\go // code storage path
    -GOROOT=c:\go // go sdk installation path
go build // Compiled language, need to compile and then execute, compile into executable file, execute executable file
go run // Compile and execute, used in the development phase, two steps and one step

go get // Download and install packages and dependencies equivalent to pip install
go version
go fmt // run gofmt to format (go fmt: automatically format code)

3、Hello Word

// single line comment

/*
multi-line comment
multi-line comment
 */


// For Go (all compiled languages) projects to run, there must be an entry
// The entry to Go is the main function under the main package

// Can there be multiple main functions under the main package?

package main // declare the package name, the package name is main, each Go file belongs to a certain package

import "fmt" // import package, built-in package

func main() { // defines a main function, the curly braces are the content of the function body
	fmt.Println("Hello World") // print function is equivalent to print()
}



// compile
go build s1.go
// implement
s1.exe

// compile并执行
go run s1.go
// In goland, right click and run

2. Variables

There are three main ways to define variables in the go language, and they cannot be defined repeatedly.

How to define

1. Method 1: Full Definition

// var variable name variable type = variable value

package main	

import "fmt"

func main() {
	var age int = 10 // In go, variables must be used when they are defined, and an error will be reported if they are not used
	fmt.Println(age)
}

2. Method 2: Type deduction (the type does not need to be written)

package main

import "fmt"

func main() {
   var age = 20
   var name = "XiaoYang"
    
   fmt.Println(age, name)
   fmt.Printf("%T\n",name) // Check the variable type\n means newline
   fmt.Printf("%p",&name) // View variable memory address
}

// output
20 XiaoYang
string
0xc000056230

3. Method 3: Abbreviated declaration (type and var keywords are not written)

package main

import "fmt"

func main() {
   age := 20
   var age int = 30 // Repeated definitions will report an error, and cannot be repeated
    
   fmt.Println(age)
}

4. Other definition methods: Other definition methods are derived from the deformation on the basis of the first three methods:

Only define without assignment

var age int // Define variables, if you only define and do not assign values, you can only use this way

var age // error, cannot determine type

declare multiple variables

var width, height int = 100, 50 // declare multiple variables
var width, height = 100, 50 // declare multiple variables
var width, height = 100, "XiaoYang" // declare multiple variables
width, height := 100, "XiaoYang" // declare multiple variables

Declare multiple variables and assign initial values

var (
   name = "XiaoYang"
   age int = 20
   height int
)
fmt.Println(name, age, height)

Notice:

var age int = 20

name, age := "XiaoYang", 21		
// In this case, no error will be reported, we think it is a repeated definition, as long as there is an undefined variable on the left side of the colon.

fmt.Println(name, age)

Summarize:

  1. The variable type is determined at the definition stage, once determined, it is not allowed to change
  2. Variables cannot be defined repeatedly
  3. Variables must be defined first before using

Variable Definition Specification

  • Variable naming should use camel case as much as possible (case has special meaning)
  • Go file naming suggests underscore
  • A name must start with a letter (Unicode letter) or underscore and can be followed by any letter, number or underscore
  • Uppercase and lowercase letters are different: Name and name are two different variables
  • Neither keywords nor reserved words are recommended for variable names

3. Constants

The definition of the constant definition is defined using the const keyword, and changes are not allowed

method one:

package main

import "fmt"

func main() {
   const age int = 20 // Modification is not allowed, modification will report an error

   fmt.Println(age)
}

Method 2: Type inference

package main

import "fmt"

func main() {
   const age = 20 

   fmt.Println(age)
}

Note: You cannot use := to define this way, this is to define a variable

4. Basic data types

1. Numbers

// signed integer
int: int32 on 32-bit machines, int64 on 64-bit machines
int8: Indicates that the range of integers is: 8 bits, 8 bits are 1byte, the first bit represents the positive and negative numbers 0/1, so it is the range of 2 to the 7th power -1
int16: 2 to the 15th power minus one
int32: 2 to the 32nd power minus one
int64: 2 to the 64th power minus one
byte: equal to int8
short: equal to int16

// unsigned integer
uint8: There is no negative number representation, so it is 2 to the 8th power minus one
uint16: same as above
uint32:
uint64:

2. Floating point type (decimal, 32/64 indicates how many digits after the decimal point)

float32
float64

3. Bool

// Data type default value:

Number type is 0
String type is empty string
boolean false

4. String

// wrapped in double quotes
// wrapped in single quotes