Golang variables: Difference between revisions
From wikinotes
(→iota) |
(→iota) |
||
Line 44: | Line 44: | ||
allowing you to use constants similar to an enum | allowing you to use constants similar to an enum | ||
The type this constant takes is inferred based on the types it is used with. | {{ expand | ||
| The type this constant takes is inferred based on the types it is used with. | |||
| | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
const ( | const ( | ||
red = iota // 0 | red = iota // 0 (because var usage below) | ||
green // 1 | green // 1 | ||
blue // 2 | blue // 2 | ||
Line 54: | Line 56: | ||
var color int = red // types within the group will be int now | var color int = red // types within the group will be int now | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
Line 73: | Line 76: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{ expand | |||
| Set iota initial-value | |||
| | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
const ( | const ( | ||
Line 82: | Line 87: | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
{{ expand | |||
| Assignment Patterns (ex. unit increases) | |||
| | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
const ( | const ( | ||
Line 92: | Line 100: | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
{{ expand | |||
| Permission Bitmasks | |||
| | |||
<syntaxhighlight lang="go"> | |||
const ( | |||
r = 1 << iota // 0b001 | |||
w // 0b010 | |||
x // 0b100 | |||
) | |||
userPermissions := read | write | |||
if (userPermissions & read) { | |||
fmt.Println("user can read file!") | |||
} | |||
</syntaxhighlight> | |||
}} | |||
</blockquote><!-- iota --> | </blockquote><!-- iota --> | ||
</blockquote><!-- Constants --> | </blockquote><!-- Constants --> |
Revision as of 18:57, 29 May 2022
Assignment
// declare and assign variable var name string name = "foo" // declare and assign var in one step var name string = "foo" // declare and assign variable, inferring type name := "foo"
Constants
Basics
const myVar := "hi" // variable that cannot be reassigned const MyVar := "hi" // exported variable, that cannot be reassigned const myVar = 2 // infer constant type const ( // group constant assignment name string = "foo" age int = 123 )There are some rules for constant:
- Constants cannot be assigned at runtime (ex. the result of a function). They must be static at compile time.
- Constants must be assigned a immutable type (ex. collections are mutable, so they cannot be constants)
- Inner scopes can declare the same constant with a new value. It will superseed the outer constant's value while working within that scope.
- If inferring a constant, it's type may take on the type of an operation it is used with. (likely best to explicitly declare type)
iota
iota
will auto-increment the value of the assignment,
allowing you to use constants similar to an enumThe type this constant takes is inferred based on the types it is used with.
const ( red = iota // 0 (because var usage below) green // 1 blue // 2 ) var color int = red // types within the group will be int now
Since the type here is inferred, comparing a variable with no value to the first entry will return true. for this reason, the first const in a iota group is generally discarded or used as an error value.// ex. // const ( red = iota; green; blue ) // var i int // i == red // returns true <--- Not what we want!! const ( _ = iota // this is our trash value -- it's inaccessible red // 1 green // 2 blue // 3 )Set iota initial-value
const ( _ = iota + 10 red // 11 green // 12 blue // 13 )Assignment Patterns (ex. unit increases)
const ( _ = iota KB = 1 << (10 * iota) // 1024 MB // 1024^2 GB // 1024^3 )Permission Bitmasks
const ( r = 1 << iota // 0b001 w // 0b010 x // 0b100 ) userPermissions := read | write if (userPermissions & read) { fmt.Println("user can read file!") }
Type Conversion
float32(123) == 123. // cast int as float32 string(107) == "k" // retrieve char for 107 in ascii chart strconv.Itoa(107) == "107" // represent 107 as string
Introspection
fmt.Prinf("%T\n", myVar) // print type of myVar
Mutability
Mutable
- Arrays - Maps - Channels - StructsImmutable
- Interfaces - Booleans - Numeric Types - Strings - Pointers