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

How the Go language declares variables

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how the Go language declares variables, I believe 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!

I. Overview

The function of the variable is to store the user's data

Second, declare variables

Every variable in the Go language has its own type and must be declared before it can be used

Declaration format of variables:

Var [variable type]

Var an int / / declares an integer value var b string / / declares a variable of string type var c float32 / / declares a variable of 32-bit floating-point slice type, which represents a data structure consisting of multiple floating-point types var d func () bool / / declares a function variable that returns a Boolean value This form is generally used for the callback function var e struct {/ / to declare a variable of structure type x int}.

a. Standard format

Starts with the keyword var, follows the variable type, and no semicolon is required at the end of the line

Var variable name variable type

b. Batch format

Var (an int b string c float32 d func () bool e struct {x int}) III. The format of the type deduced by the compiler [must be assigned]

After omitting the type, the compiler attempts to derive the type of the variable from the expression to the right of the equal sign

Var hp = 1004. Short variables are declared and initialized

Omit the type, var, and change = to =:

Because the ": =" is used instead of the assigned "=", the left-value variable that deduces the declaration must be an undefined variable.

If defined, a compilation error will occur

Xp: = 10fptraining apprentices20,305. Anonymous variables-variables without names

When using multiple assignments, you can use anonymous variables if you do not need to receive variables in the left value.

Anonymous variables are represented by an underline of "_". When using anonymous variables, you only need to replace them with an underline where the variable is declared.

An attention 10, 206, attention

When the compiler deduces a type, [be sure to assign]

Because the ": =" is used instead of the assigned "=", the left-value variable that deduces the declaration must be an undefined variable. [if defined, a compilation error will occur]

It is better to have more than two variables to use anonymous variables [or lose their meaning]

Case package mainimport "fmt" import "net" func main () {/ * one, declare * / var an int / / declare a variable of integer type, var b string / / declare a variable of string type var c float32 / / declare a variable of 32-bit floating point slice type Floating-point slices represent a data structure consisting of multiple floating-point types var d func () bool / / declares a function variable that returns a Boolean value This form is generally used for the callback function var e struct {/ / to declare a variable of structure type x int} var f bool / / to declare a variable of Boolean type fmt.Printf ("a type:% T, value:% v\ n", a) / a type: int, value: 0 fmt.Printf ("b type:% T" Value:% Q\ n ", b, b) / / b type: string, value:" fmt.Printf ("c type:% T, value:% v\ n", c, c) / / c type: float32, value: 0 fmt.Printf ("d type:% T, value:% v\ n", d, d) / / d type: func () bool, value: fmt.Printf ("e type:% T") Value:% v\ n ", e, e) / / e type: struct {x int}, value: {0} fmt.Printf (" f type:% T, value:% v\ n ", f, f) / / f type: bool Value: false / * II. Batch statement * / / var (/ / an int / / b string / / c float32 / / d func () bool / / e struct {/ / x int / /} /) / / 3 The compiler deduces the format of the type / be sure to assign a value This is type derivation at compile time] var hp = 100fmt.Println (hp) fp,ap:=20,30 fmt.Println (fp,ap) / / IV, declare and initialize short variables / / hp: = 10 / / error no new variables on left side of: = No new variables appear on the left side of ": =" The left variable meaning ": =" has been declared by multiple short variables [compiler will not repeat err] conn1,err: = net.Dial ("tcp", "127.0.0.1 net.Dial 8080") conn2, err: = net.Dial ("tcp", "127.0.0.1 net.Dial 8080") fmt.Println (conn1,err) fmt.Println (conn2) Err) / / 5. Anonymous variables-- variables without names are all the contents of the article entitled "how to declare variables in Go language" above. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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