Go Variables: Difference between revisions
(→Naming) |
(→Naming) |
||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
When choosing variable names, try to make them work well with their enclosing package name. See: {{Internal|Go_Packages#Try_to_Make_Package_Names_and_Exported_Names_Work_Together|Try to Make Package Names and Exported Names Work Together}} | |||
Go [[#(No)_Reference_Variables|does not have reference variables]], but it does have pointers, so one may assume that there are naming conventions to make obvious when a variable contains a pointer, and when it contains a value. In fact, there aren't such conventions. Pointer and value variable names are indistinguishable. For more details, see: {{Internal|Pointers_in_Go#Pointer_Variable_Naming|Pointer Variable Naming}} | Go [[#(No)_Reference_Variables|does not have reference variables]], but it does have pointers, so one may assume that there are naming conventions to make obvious when a variable contains a pointer, and when it contains a value. In fact, there aren't such conventions. Pointer and value variable names are indistinguishable. For more details, see: {{Internal|Pointers_in_Go#Pointer_Variable_Naming|Pointer Variable Naming}} |
Revision as of 01:49, 4 July 2024
External
Internal
Overview
Variables are names associated with memory locations that store values. Variable name rules are described in the Naming section. Each variable has a type. Because Go is a statically typed language, the type associate with a variable may not change after the declaration. Each variable defined in Go occupies a unique memory location. It is not possible to have two variables that share the same storage location. It is possible to have two variable that point to the same storage location, in case of pointer variables, but this is not the same thing as sharing the same storage location. In other words, Go has no reference variables.
Naming
The names of variable identifiers in Go follow general naming conventions for identifiers.
Variable names should be short rather than long. The closer to declaration a name is used, the shorter it should be. Conversely, the farther away from the declaration you use it, the longer the name should be. i
is fine when iterating over an array.
Two letter variable names should be used for slices or maps.
var tt []*Thing // tt is many
When choosing variable names, try to make them work well with their enclosing package name. See:
Go does not have reference variables, but it does have pointers, so one may assume that there are naming conventions to make obvious when a variable contains a pointer, and when it contains a value. In fact, there aren't such conventions. Pointer and value variable names are indistinguishable. For more details, see:
(No) Reference Variables
Go does not have reference variables. Each variable defined in Go occupies a unique memory location, which contains the value of the variable. Maps, channels, etc. variables are all values, not references. As such, there is no pass-by-referece in Go, everything is passed by value. To achieve behavior similar to pass-by-reference, use pointers. This is explained here:
Variable Declaration
The name and the type of the variable are specified in the variable declaration, which is a statement prefixed by the keyword var
. This style is some times referred to as "long declaration":
var <variable-name>[, <variable-name-2>, ...] <type>
var size int
var size, weight int
Such a variable declaration is also an implicit initialization with the zero value of the variable's type. Unless you intent to initialize the variable to a non-zero value with the short variable declaration :=
operator can be used, var var_name var_type
is the preferred syntax.
More variables can be declared at the same time with:
var (
a int
b string
...
)
A variable that can take any type can be declared using interface{}
or its equivalent any
:
var r any
Also see:
Variable Initialization after Declaration
var color string
color = "blue"
Variable Initialization in Declaration
Variables can be initialized in the declaration:
var color string = "blue"
This is redundant, and some editors will render the type name in gray and pop up a soft static analysis warning: "Type can be omitted", because an alternative syntax, initialization with type inference is available.
Variable Initialization with Type Inference
var color = "blue"
Swap Variable Values
a, b = b, a
Also works with arrays and slices.
Short Variable Declaration
The declaration and initialization can be performed together with the :=
operator. This obviates the need for the var
keyword and it deploys type inference. This kind of declaration can only be performed inside a function.
color := "blue"
The short variable declaration can be used to initialize more than one variable at the same time, when the assignment contains multiple values, or when on the right side of the assignment there is a function that returns multiple values:
i, j := 0, 1
color, size := someFuncWithTwoReturnValues(...)
Variable Declaration with new()
new()
creates a variable or a certain type, specified as the argument of the new()
function invocation, and returns a pointer pointer to that variable.
ptri := new(int)
*ptri = 3
Why do we need to involve variables here? There's no variable, new() just allocates space for an instance of a certain type and returns a pointer to it.
Creating instances with New()
Scope
The scope of a variable is defined as places in the code where the variable can be accessed. In Go, the scope of a variable can be formally defined by using the concept of block: Go is a lexically scoped language using blocks.
Given two blocks bi and bj, we say that bi ≥ bj if bj is inside (is enclosed in) bi. This relationship is transitive.
The formal definition of visibility (or accessibility) of a variable v
is: a variable v
is visible in a block bj if it is declared in a block bi so bi ≥ bj.
If a variable with the same name is declared in two blocks between which there is a an inclusion relationship, the variable defined in the "closest" block takes precedence.
Also see: