I plan to transfer from PHP to GO, keep punching in every day, keep learning new knowledge points every day, prepare for 30K + monthly salary.

compile

  • Using the go build
  1. Execute in the project directory
  2. To compile a Go build in another path, add the project path to it (the project path is written from GOPATH/ SRC, and the compiled executable is stored in the current directory).
  3. Customize the package name: go build-o Customize the package name

go run

Execute the GO code as if it were a script file

go install

Go Install consists of two steps:

  1. Compile first to get an executable file
  2. Copy executable files to ‘GOPATH/bin’

Cross-compilation Cross-platform compilation

  1. Go supports cross-platform compilation
  2. For example, compiling binaries on MAC or Windows that run on Linux
  3. Example code: Compiling 64-bit executables for Linux and Windows platforms under Mac:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Copy the code

Basic structure of the GO language file

// Only identifiers (variables, constants, functions, types) can be placed around functions; Func main() {fmt.println ("hello world")} cannot place statement // program entry function func main() {fmt.println ("hello world")}Copy the code

Variables and constants

  • Identifiers and keywords
  • The keyword
  • Reserved words

variable

Blog go variables should be declared before they are used

Declare a variable

‘var sl string’: declare a s1 variable to store string data

Matters needing attention

  1. Every statement outside a function must start with a keyword (var, const, func, etc.)
  2. := cannot be used outside functions
  3. _ is used as a placeholder and can be ignored
Package main import "FMT" // Camel name is recommended in go. // var studentName string // Declare variables. // var name string // var age int // var isOk Var (name string // "" age int // 0 isOk bool // false) // Anonymous variables are specified by _. Anonymous variables do not occupy the namespace. No memory is allocated, so there are no duplicate declarations between anonymous variables. Func main() {name = "idealage" age = 18 isOk = true // var test = string Print(isOk) FMT.Println() FMT.Printf("name:%s\n", Name) // %s is a placeholder for the %s placeholder fmt.println (age) // after printing the specified content, Var s1 string = "wzy" ftt.println (s1) var s1 string = "wzy" ftt.println (s1) var s1 string = "wzy" ftt.println (s1 Println(s2) := "luanran" Println(s3) := "luanran" Println(s3) // the same {} is a scope // s3 := "huihui" // the anonymous variable is a special variable: _}Copy the code

Basic data types

The difference between 32-bit and 64-bit operating systems

Program addressing length, how much memory can be supported, an old 32-bit Windows operating system can read 3.8 GB of memory at most, install 16 GB memory stick can read 3.8 GB of memory

Octal and hexadecimal

  • The first digit of an octal number is 0
  • The hexadecimal number starts with 0x

string

  • Strings in the Go language are wrapped in double quotes
S1 := "123" s2 := "we" s3 := "mystring"Copy the code
  • In GO, single quotation marks enclose characters (individual letters, Chinese characters, symbols, numbers)
S1 := '1' s2 := 'me' S3 := 's' // byte: 1 byte =8Bit(8 binary bits) // A character 'A' = 1 byte // A UTF8 encoded Chinese character usually takes up 3 bytes (such as' sand ')Copy the code

Persistence is victory, come on!