Summary of variable learning in Golang

  • 2020-06-19 10:30:29
  • OfStack

Golang variables are generally of 4 types

1. bool string

bool: The Boolean type, true, false
string: String type

2. (u)int, (u)int8, (u)int16, (u)int32, (u)int64, uintptr

int and uint, which have no u and u unsigned is refers to the presence of symbol, also is the presence of plus or minus, int type will be judged according to your word count of the operating system is 32 bit or 64 bit, if your operating system is 64 - bit, then when defining int is 64, that is, when you define int compiler will according to your operating system to automatically assigned to this variable much space.
(u)int8, (u)int16, (u)int32, (u)int64 the following 8, 16, 32, 64 refer to the size of the variable you define. 8 is 8 bits, and the 8 bits here refer to 8 bits in base 2. The same is true for 16, 32, 64.
uintptr is a pointer type, and the Go language also has pointer 1 features, but is not as complex as the c language. We'll get into this type later.

3. byte, rune

The byte type you can think of as an individual name for uint8, byte and uint8 represent the same range, also known as the byte type, byte, and the maximum range that can be represented is the size of a byte.
rune is a new data type, because you have not seen in other languages, this type is relatively unfamiliar, but I, for example, you can fully understand, in c language, have char this type, is used to is a ASCII character, but with the popularity of programming knowledge, people in many countries are learning to program, write a program, but the language will become to 1 big obstacle, using only ASCII coding is far cannot satisfy the demand of people, so the Go language is to use the Unicode coding, rune is the equivalent of the character type in Unicode.
The length of rune is 32 bits, we all know that unicode is 2 bytes, but es62EN-8 encoding is used most of the time in the Internet, so our rune type USES 4 bytes of storage space, if later Go will upgrade to ES65en-8 encoding provides the possibility.
In c, we all know that char can be mixed with integer, for example: printf("%c",97); The output is the lowercase a. The rune type can be used similarly.

4. float32, float64, complex64, complex128

Both float32 and float64 are floating-point types, and float32 provides approximately 6 decimal places, compared to float64, which provides 15 decimal places. If you want to explore the way floating-point Numbers are stored on a computer, you can do this by a factor of one.
complex64 and complex128 are used to represent complex Numbers in our mathematics, the real and imaginary parts of complex Numbers, the real and imaginary parts of complex64 are both 32-bit float, and the real and imaginary parts of complex128 are both 64-bit float.

How Golang's variables are defined


package main

import "fmt"

var bb uint8 = 12
var xx = 12
var (
  cc = 12
  q, w = 12, 12
  b string = "false"
)
func variableZoreValue() {
  var a int
  var b string
  fmt.Println(a, b)
}

func variableInitialValue() {
  var a int = 1
  n, x := 1, 2
  var s string = "abc"
  fmt.Println(a, s)
  fmt.Println(n, x)
}
func main() {
  variableZoreValue()
  variableInitialValue()
  fmt.Println(bb, xx, cc, q, w, b)
}

The variables we define can be in or out of a function, and the variables defined outside of a function are not global variables, they should be called in-package variables because they are under 1 package.

Define 1 variable:

var a int this definition specifies the type of variable, not initialized, but our compiler will initialize it to 0 for you
var a int = 16 this is more initialization than the previous one.
var a = 12 does not specify a type, but initializes a value of 12. In this way, the compiler will automatically identify the type, just like in python, there is no need to specify a type.
a := 12 this method is similar to the previous one, but is more convenient. You don't need to write var.

Define multiple variables

var bb,cc uint8 = 12, 13 This way you can define multiple variables of the same type
var z = 14, str = "string" is better than the previous one because it allows you to define several different types of data at the same time.
n, x := 1, 2 this approach is more convenient than the previous one, which allows you to define multiple variables of different types simultaneously without having to write the var keyword.

complex plural type


package main

import (
  "fmt"
  "math/cmplx"
  "math"
)

func complexVariable() {
  c := 3 + 4i
  fmt.Println("|3 + 4i|=",cmplx.Abs(c))
  fmt.Println(cmplx.Pow(math.E, 1i * math.Pi) + 1)
  fmt.Println(cmplx.Exp(1i * math.Pi) + 1)
  fmt.Printf("%0.3f", cmplx.Exp(1i * math.Pi) + 1)
}
func main() {
  complexVariable()
}

c := 3 + 4i, instead of: c := 3 + 4*i, the compiler will treat this i as a variable rather than a plural flag
cmplx.Abs(c), take the absolute value of the plural
cmplx.Pow(math.E, 1i * math.Pi) +1, to calculate e^(i)+1,e is the natural number in mathematics.
cmplx.Exp(1i * math.Pi) + 1, another calculation method
fmt. Printf("%0.3f", ES166en. Exp(1i * math. Pi) + 1) format output plural.

Definition of constants


package main

import (
  "fmt"
  "math"
)
c consts() {
  var filename = "1.txt"
  const a, b = 3, 4
  var c int = 0
  c = int(math.Sqrt((a*a + b*b)))
  fmt.Println(filename, c)
}
func main() {
  consts()
}

A constant in Go is the same as a macro constant in c, so if the compiler does not specify a type when defining a constant, there is no need to convert (a*a + b*b)) strong to float64.
Constants are also true in the rules that define variables, and I won't go into that, but I'll see for myself.

Define enumerated types

There is no keyword for enumeration types in Go. How does the Go language define enumeration types?


package main

import "fmt"

func enums() {
  const(
    c = 0
    cpp = 1
    java = 2
    python = 3
  )
  fmt.Println(c, cpp, java, python)
}
func main() {
  enums()
}

This allows us to define an enumeration and also use the iota keyword to indicate that the enumeration type is increasing


package main

import "fmt"

func enums() {
  const(
    c = iota
    cpp
    java
    python
  )
  fmt.Println(c, cpp, java, python)
}
func main() {
  enums()
}

More advanced definition methods:


//  define b kb mb gb tb pb
const(
  b = 1 << (10 * iota)
  kb
  mb
  gb
  tb
  pb
)

iot wants to add values from zero, and you can define constants in this way for any complex enumeration type you want us to write out expressions.

conclusion

[

In the Go language variable constant definition, the type identification of the variable is written after the variable name, the compiler can automatically infer that you need to define the type of the variable, if you want to initialize the variable, there is no char only rune, the original support for the complex type.

]


Related articles: