SoFunction
Updated on 2025-03-05

Go language variables and basic data types details

1. Basic introduction

Go is a static (compiled) language, a weak-type language that is different from interpreted languages ​​(static: fixed type, strong type: different types do not allow direct operation)

For examplepython It's a dynamically strong language

1. Characteristics of Go

Cross-platform compilation language, cross-compilation

pipeline(channel),slice(slice),concurrent(routine

There is a garbage collection mechanism

Supports object-oriented and process-oriented programming modes (Go's object-oriented does not have the concept of class)

2. Common commands of Go

go env   // Go environment variables    -GO111MODULE=     // Empty, no MODULE mode is used now    -GOPATH=C:\Users\oldboy\go  // Code storage path    -GOROOT=c:\go     // go sdk installation pathgo build   // Compiled languages ​​need to be compiled first and then executed, compiled into an executable file, and execute executable filego run   // Compile and execute, use it in the development stage, take two steps at the same time
go get   // Download and install packages and dependencies are equivalent to pip installgo version
go fmt   // Run gofmt to format (go fmt: automatically format the code)

3、Hello Word

// Single line comment
/*
 Multi-line comments
 Multi-line comments
  */


// To run Go (all compiled languages) projects, there must be an entry// The entrance 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, and each Go file belongs to a certain package
import "fmt"    // Import package, built-in package
func main() {   // Define a main function, the braces wrap are the contents of the function body ("Hello World")  // Print function is equivalent to print()}

 

// Compilego build 
// implement

// Compile and executego run 
// In goland, right-click and run

2. Variables

The variable definition methods of go language are mainly divided into three ways and cannot be defined repeatedly.

1. Definition method

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 if they are defined, and if they are not used, an error will be reported (age)
}

Method 2: Type derivation (types are not required)

package main

import "fmt"

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

// Output20 XiaoYang
string
0xc000056230

Method 3: Brief declaration (non-write type and var keywords)

package main

import "fmt"

func main() {
   age := 20
   var age int = 30   // Repeat definition will cause an error, and it cannot be repeated    
   (age)
}

Other definition methods: Other definition methods are based on the first three methods and are transformed:

Only define and do not assign values:

var age int // Define variables. If you only define and do not assign values, you can only use this method
var age  // Error, unable to determine the type

Declare multiple variables:

var width, height int = 100, 50  // Declare multiple variablesvar width, height = 100, 50   // Declare multiple variablesvar width, height = 100, "XiaoYang" // Declare multiple variableswidth, height := 100, "XiaoYang" // Declare multiple variables

Declare multiple variables and assign initial values:

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

Notice:

var age int = 20

name, age := "XiaoYang", 21  
// This situation will not report an error. We think it is a repeated definition. As long as there is an undefined variable on the left side of the colon.
(name, age)

Summarize:

  • The variable type is determined during the definition stage, and once it is determined, it is not allowed to change.
  • Variables cannot be defined repeatedly
  • Variables must be defined first in use
  • Variable definition specification
  • Try to use camels when creating variable naming (the upper case has special significance)
  • It is recommended to use underlined Go file naming
  • A name must be in one letter (UnicodeLetter) or underscore begins with any letter, number or underscore
  • Capital letters and lowercase letters are different:Name andname are two different variables
  • Neither keyword nor reserved word is recommended as variable names

3. Constant

Use of the definition of constant definitionconst Keyword definition, no change allowed

Method 1:

package main

import "fmt"

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

Method 2: Type Deduction

package main

import "fmt"

func main() {
   const age = 20 

   (age)
}

Notice:You cannot use := to define it, so you can define variables

4. Basic data types

1. Numbers

// Signed plastic surgeryint: exist32The bit machine isint32,exist64The bit machine isint64
int8: Indicates that the integer range is:8One bit,8indivualbityes1byte ,The first digit represents the positive and negative number0/1, 所以yes2of7To the power-1 of范围
int16: 2of15To the power减一
int32: 2of32To the power减一
int64: 2of64To the power减一
byte: equalint8
short: equalint16

// Unsigned plastic surgeryuint8: No negative representation,所以yes2of8To the power减一
uint16: Same as above
uint32:
uint64:

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

float32
float64

3. Bool

// Data type default value:
The number type is  0
The string type is    Empty string
Boolean type  false

4. String

// Double quotes wrapped// Single quotes wrap

This is the end of this article about the details of Go variables and basic data types. For more related Go variables and basic data types, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!