Home  >  Article  >  Backend Development  >  How different variable types are defined in Golang

How different variable types are defined in Golang

王林
王林Original
2024-01-18 08:23:161146browse

How different variable types are defined in Golang

The way to define different types of variables in Golang requires specific code examples

In Golang, we can use different ways to define different types of variables. This article will introduce how to define several common variable types in Golang and provide corresponding code examples.

1. Integer variable (int)
Integer variables in Golang can be used to store integer values. They are defined as follows:

var num int // Declare one Integer variable named num
num = 10 // Assignment
fmt.Println(num) // Output: 10

2. Floating point variable (float)
In Golang Floating-point variables can be used to store real values, and are defined as follows:

var num float64 // Declare a floating-point variable named num
num = 3.14 // Assignment
fmt.Println(num) // Output: 3.14

3. Boolean variable (bool)
Boolean variables in Golang are used to store true and false values, and are defined as follows:

var flag bool // Declare a Boolean variable named flag
flag = true // Assign value
fmt.Println(flag) // Output: true

4 .String variable (string)
String variables in Golang are used to store character sequences. They are defined as follows:

var str string //Declare a string variable named str
str = "Hello World" // Assignment
fmt.Println(str) // Output: Hello World

5. Array variable (array)
The array in Golang is a fixed The length and sequence of elements of the same type are defined as follows:

var arr [5]int // Declare an integer array named arr with a length of 5
arr = [5] int{1, 2, 3, 4, 5} // Assignment
fmt.Println(arr) //Output: [1 2 3 4 5]

6. Slice variable (slice)
A slice in Golang is a reference to an array and can grow dynamically. It is defined as follows:

var sli []int // Declare an integer slice named sli
sli = []int{1, 2, 3} // Assignment
fmt.Println(sli) // Output: [1 2 3]

7. Dictionary variable (map)
in Golang A dictionary is an unordered collection of key-value pairs, which is defined as follows:

var m map[string]int // Declare a dictionary named m from string to int
m = map[string]int{"a": 1, "b": 2} // Assignment
fmt.Println(m) // Output: map[a:1 b:2]

8. Structure variable (struct)
The structure in Golang is a custom data type that can combine different fields. It is defined as follows:

type Person struct {

Name string
Age  int

}

var p Person // Declare a Person structure variable named p
p = Person{Name: "Tom", Age: 20} // Assign value
fmt .Println(p) // Output: {Tom 20}

9. Pointer variable (pointer)
Pointer variables in Golang are used to store memory addresses pointing to other variables. They are defined as follows :

var ptr *int // Declare an integer pointer variable named ptr
num := 10
ptr = &num // Assign value
fmt.Println(ptr) // Output: 0xc000014088

The above are the definition methods of some common types of variables in Golang and the corresponding code examples. Through these examples, we can better understand and use different types of variables in Golang. Hope this article can be helpful to you.

The above is the detailed content of How different variable types are defined in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn