Golang variables: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 1: Line 1:
= Environment Variables =
= Process Variables =
<blockquote>
== Call Parameters ==
<blockquote>
commandline arguments are stored in <code>os.Args</code>
 
<syntaxhighlight lang="go">
for _, arg := range os.Args[1:] {
    switch arg {
    case "-h", "--help":
        // .. print help
    case "-v", "--verbose":
        // .. enable verbose logging
    }
}
</syntaxhighlight>
</blockquote><!-- Call Parameters -->
 
== Environment Variables ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 9: Line 27:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Environment Variables -->
</blockquote><!-- Environment Variables -->
</blockquote><!-- Process Variables -->


= Assignment =
= Go Variables =
<blockquote>
== Assignment ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 25: Line 46:
</blockquote><!-- Assignment -->
</blockquote><!-- Assignment -->


= Scope =
== Scope ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 58: Line 79:
</blockquote><!-- Scope -->
</blockquote><!-- Scope -->


= Type Conversion =
== Type Conversion ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 67: Line 88:
</blockquote><!-- Type Conversion -->
</blockquote><!-- Type Conversion -->


= Introspection =
== Introspection ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 75: Line 96:




= Constants =
== Constants ==
<blockquote>
<blockquote>
== Basics ==
=== Basics ===
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
Line 100: Line 121:
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->


== iota ==
=== iota ===
<blockquote>
<blockquote>
See enums in [[golang datatypes]]
See enums in [[golang datatypes]]
Line 106: Line 127:
</blockquote><!-- Constants -->
</blockquote><!-- Constants -->


= Mutability =
== Mutability ==
<blockquote>
<blockquote>
== Mutable ==
=== Mutable ===
<blockquote>
<blockquote>
<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">
Line 118: Line 139:
</blockquote><!-- Mutable -->
</blockquote><!-- Mutable -->


== Immutable ==
=== Immutable ===
<blockquote>
<blockquote>
<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">
Line 129: Line 150:
</blockquote><!-- Immutable -->
</blockquote><!-- Immutable -->
</blockquote><!-- Mutability -->
</blockquote><!-- Mutability -->
</blockquote><!-- Go Variables -->

Revision as of 22:20, 18 June 2022

Process Variables

Call Parameters

commandline arguments are stored in os.Args

for _, arg := range os.Args[1:] {
    switch arg {
    case "-h", "--help":
        // .. print help
    case "-v", "--verbose":
        // .. enable verbose logging
    }
}

Environment Variables

import "os"

path, err := os.LookupEnv("HOME")  // "/home/will"
err := os.SetEnv("FOO", "BAR")
vars = os.Environ()                // ["FOO=BAR", "BAR=BAZ", ...]

Go Variables

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"

Scope

// global variables are defined outside of functions
foo_global := "bar"

// local variables defined in functions
func main() {
    foo_local := "bar"
}

go supports closures (although to be async friendly, it is recommended to explicitly pass params)

func main() {
    a := 123
    func() {
        fmt.Println("value of a:", a)
    }
}

Exported symbols (functions, variables) begin with a capital letter.
this means they can be used by files that import this package.

Foo := "bar"
func MyPublicFunc() { ... }

Exported symbols from packages defined within an internal/ directory are only exported within your go module.
Consumers of your go module and it's packages will not have access to these symbols.

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


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

See enums in golang datatypes

Mutability

Mutable

- Arrays
- Maps
- Channels
- Structs

Immutable

- Interfaces
- Booleans
- Numeric Types
- Strings
- Pointers