Golang variables: Difference between revisions

From wikinotes
No edit summary
Line 32: Line 32:
= Mutability =
= Mutability =
<blockquote>
<blockquote>
== Mutable ==
<blockquote>
<syntaxhighlight lang="yaml">
- Arrays
- Maps
- Channels
- Structs
</syntaxhighlight>
</blockquote><!-- Mutable -->


== Immutable ==
<blockquote>
<syntaxhighlight lang="yaml">
- Interfaces
- Booleans
- Numeric Types
- Strings
- Pointers
</syntaxhighlight>
</blockquote><!-- Immutable -->
</blockquote><!-- Mutability -->
</blockquote><!-- Mutability -->

Revision as of 13:42, 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"

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
- Structs

Immutable

- Interfaces
- Booleans
- Numeric Types
- Strings
- Pointers