mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 21:16:21 +00:00
231 lines
4.1 KiB
Markdown
231 lines
4.1 KiB
Markdown
---
|
|
id: ef9ed7f2-0bee-4405-a6d2-993cdea80029
|
|
title: Golang variables
|
|
---
|
|
|
|
# Basics
|
|
|
|
The \`var\` statement declares a list of variables. Type is last. A
|
|
\`var\` statement can be at package or function level. Use var when you
|
|
to set a variable type but don't want to set the variable value yet.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
var c, python, java bool
|
|
|
|
func main() {
|
|
var i int
|
|
fmt.Println(i, c, python, java)
|
|
}
|
|
```
|
|
|
|
This also works with [interfaces](20200831171822-interfaces).
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type printSomething interface {
|
|
print()
|
|
}
|
|
|
|
type agent struct {
|
|
}
|
|
|
|
func (d *agent) print() {
|
|
fmt.Println("tralala")
|
|
}
|
|
|
|
var (
|
|
a = &agent{}
|
|
)
|
|
|
|
func main() {
|
|
a.print()
|
|
}
|
|
```
|
|
|
|
# Initializers
|
|
|
|
A var declaration can include initializers, one per variable. If an
|
|
initializer is present, the type can be omitted; the variable will take
|
|
the type of the initializer.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
var i, j int = 1, 2
|
|
|
|
func main() {
|
|
var c, python, java = true, false, "no!"
|
|
fmt.Println(i, j, c, python, java)
|
|
}
|
|
```
|
|
|
|
# Short variable declarations
|
|
|
|
Inside a function, the := short assignment statement can be used in
|
|
place of a var declaration with implicit type. Outside a function, every
|
|
statement begins with a keyword (var, func, and so on) and so the :=
|
|
construct is not available.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
var i, j int = 1, 2
|
|
k := 3
|
|
c, python, java := true, false, "no!"
|
|
|
|
fmt.Println(i, j, k, c, python, java)
|
|
}
|
|
```
|
|
|
|
# Basic types
|
|
|
|
Go's basic types are
|
|
|
|
bool
|
|
|
|
string
|
|
|
|
int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr
|
|
|
|
byte // alias for uint8
|
|
|
|
rune // alias for int32 // represents a Unicode code point
|
|
|
|
float32 float64
|
|
|
|
complex64 complex128
|
|
|
|
The example shows variables of several types, and also that variable
|
|
declarations may be "factored" into blocks, as with import statements.
|
|
|
|
The int, uint, and uintptr types are usually 32 bits wide on 32-bit
|
|
systems and 64 bits wide on 64-bit systems. When you need an integer
|
|
value you should use int unless you have a specific reason to use a
|
|
sized or unsigned integer type.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/cmplx"
|
|
)
|
|
|
|
var (
|
|
ToBe bool = false
|
|
MaxInt uint64 = 1<<64 - 1
|
|
z complex128 = cmplx.Sqrt(-5 + 12i)
|
|
)
|
|
|
|
func main() {
|
|
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
|
|
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
|
|
fmt.Printf("Type: %T Value: %v\n", z, z)
|
|
}
|
|
```
|
|
|
|
# Zero values
|
|
|
|
Variables declared without an explicit initial value are given their
|
|
zero value.
|
|
|
|
The zero value is:
|
|
|
|
- 0 for numeric types
|
|
- false for the boolean type
|
|
- "" for strings
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
var i int
|
|
var f float64
|
|
var b bool
|
|
var s string
|
|
fmt.Printf("%v %v %v %q\n", i, f, b, s)
|
|
}
|
|
```
|
|
|
|
# Type conversions
|
|
|
|
The expression \`T(v)\` converts the value v to the type T.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
func main() {
|
|
var x, y int = 3, 4
|
|
var f float64 = math.Sqrt(float64(x*x + y*y))
|
|
var z uint = uint(f)
|
|
fmt.Println(x, y, z)
|
|
}
|
|
```
|
|
|
|
# Type inference
|
|
|
|
When declaring a variable without specifying an explicit type (either by
|
|
using the := syntax or var = expression syntax), the variable's type is
|
|
inferred from the value on the right hand side.
|
|
|
|
When the right hand side of the declaration is typed, the new variable
|
|
is of that same type:
|
|
|
|
``` go
|
|
var i int
|
|
j := i // j is an int
|
|
```
|
|
|
|
But when the right hand side contains an untyped numeric constant, the
|
|
new variable may be an int, float64, or complex128 depending on the
|
|
precision of the constant:
|
|
|
|
``` go
|
|
i := 42 // int
|
|
f := 3.142 // float64
|
|
g := 0.867 + 0.5i // complex128
|
|
```
|
|
|
|
# Constants
|
|
|
|
Constants are declared like variables, but with the const keyword.
|
|
|
|
Constants can be character, string, boolean, or numeric values.
|
|
|
|
Constants cannot be declared using the := syntax.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
const Pi = 3.14
|
|
|
|
func main() {
|
|
const World = "世界"
|
|
fmt.Println("Hello", World)
|
|
fmt.Println("Happy", Pi, "Day")
|
|
|
|
const Truth = true
|
|
fmt.Println("Go rules?", Truth)
|
|
}
|
|
```
|