Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of variable declaration and assignment in go language

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the example analysis of variable declaration and assignment in go language, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Variable

A variable is an abstract description of a space in memory. The type of variable defines the size of the space and how it is parsed.

Variable type 1 in Go

Bool byte complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr

Declaration and assignment of variables 11

two

three

four

five

six

seven

eight

nine

Package main

Import "fmt"

Func main () {

Var x string

X = "Hello World"

Fmt.Println (x)

}

Mode 2

Note that x: = "Hello World" is equivalent to var x = "Hello World"

Automatically infers the type and must be inside the function body

one

two

three

four

five

six

seven

eight

Package main

Import "fmt"

Func main () {

X: = 1

Fmt.Println (x)

}

Multiple assignments 1

two

three

four

five

six

seven

eight

Var i int

Var U, V, W float64

Var k = 0

Var x, y float32 =-1,-2

Var (

I int

U, v, s = 2.0,3.0, "bar"

)

Example:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

Package main

Import "fmt"

Func main () {

Var a string= "jonson", "jackson"

C true,false d: =

Erecent fjorg: = "jonson", true,123

Fmt.Println ("a:", a)

Fmt.Println ("b:", b)

Fmt.Println ("c:", c)

Fmt.Println ("d:", d)

Fmt.Println ("e:", e)

Fmt.Println ("f:", f)

Fmt.Println ("g:", g)

}

Zero value problem

If the variable initialization does not assign a value, the default is empty.

Example:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

Package main

Import "fmt"

Func main () {

Var an int

Var b string

Var c float64

Var d bool

Fmt.Printf ("% v\ n", a)

Fmt.Printf ("% v\ n", b)

Fmt.Printf ("% v\ n", c)

Fmt.Printf ("% v\ n", d)

Fmt.Println ()

}

Constant

Once a constant is declared, it cannot be changed, and the constant must be assigned an initial value. This code is invalid func main () {const x int}

Valid:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

Package main

Const (

M = 1

N = 2

)

Func main () {

Const k = 8

}

Iota usage

Iota loops from 0

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

Const (

A = iota

B = iota

C = iota

D = iota

)

Equivalent to:

Const (

A = iota

B

C

D

)

Use case:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

Package main

Import "fmt"

Func main () {

Const (

A = iota / / 0

B / / 1

C / / 2

D = "ha" / / Independent value, iota + = 1

E / / "ha" iota + = 1

F = 100 / / iota + = 1

G / / 100 iota + = 1

H = iota / / 7, restore count

I / / 8

)

Fmt.Println (a recalcitrance brecrium crecinct d recorder e recorder f g jiggy pr I)

}

Abnormal iota1

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

Package main

Import "fmt"

Const (

I, 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report