<jupyter_start><jupyter_text>01 package-scope<jupyter_code>package main

import "fmt"

var x = 42

func main() {
    
    fmt.Println(x)
    foo()
}

func foo() {
    fmt.Println(x)
}
main()
package main

import (
    "fmt"
    "github.com/GoesToEleven/GolangTraining/04_scope/01_package-scope/02_visibility/vis"
)

func main() {
    fmt.Println(vis.MyName)
    vis.PrintVar()
}
main()<jupyter_output>Todd
Todd
Future Rock Star Programmer<jupyter_text>02 block-scope  this does not compile<jupyter_code>package main

import "fmt"

func main() {
    x := 42
    fmt.Println(x)
    foo()
}

func foo() {
    // no access to x
    // this does not compile
    fmt.Println(x)
}
main()<jupyter_output>42
42<jupyter_text>closure<jupyter_code>package main 

import "fmt"

func main() {
    
    x := 42
    fmt.Println(x)
    {
        fmt.Println(x)
        y := "The credit belongs with the one who is in the ring."
        fmt.Println(y)
    }
    // outside scope of y
    //fmt.Println(y) // 会报错 repl.go:14:17: undefined identifier: y
}
main()
package main

import "fmt"

var x = 0

func increment() int {
    x++
    return x
}

func main() {
    
    fmt.Println(increment())
    fmt.Println(increment())
}
main()

/*
closure helps us limit the scope of variables used by multiple functions
without closure, for two or more funcs to have access to the same variable,
that variable would need to be package scope
*/
package main

import "fmt"

func main() {
	x := 0
	increment := func() int {
		x++
		return x
	}
	fmt.Println(increment())
	fmt.Println(increment())
}
main()
/*
closure helps us limit the scope of variables used by multiple functions
without closure, for two or more funcs to have access to the same variable,
that variable would need to be package scope
anonymous function
a function without a name
func expression
assigning a func to a variable
*/
package main

import "fmt"

func wrapper() func() int {
	x := 0
	return func() int {
		x++
		return x
	}
}

func main() {
	increment := wrapper()
	fmt.Println(increment())
	fmt.Println(increment())
}
main()
/*
closure helps us limit the scope of variables used by multiple functions
without closure, for two or more funcs to have access to the same variable,
that variable would need to be package scope
*/<jupyter_output>1
2<jupyter_text>03 order-matters<jupyter_code>package main

import "fmt"

func main() {
	fmt.Println(x)
	fmt.Println(y)
	x := 42
}

var y = 42

main()<jupyter_output>2
42<jupyter_text>04 variable-shadowing<jupyter_code>package main

import "fmt"

func max(x int) int {
	return 42 + x
}

func main() {
	max := max(7)
	fmt.Println(max) // max is now the result, not the function
}

// don't do this; bad coding practice to shadow variables

main()
package main

var x = 7
package main

import "fmt"

func main() {
	fmt.Println(x)
}
main()<jupyter_output>7
{'fraction_non_alphanumeric': 0.11017247448081662, 'fraction_numerical': 0.01583949313621964, 'mean_word_length': 3.440625, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '24966', 'license': 'MIT', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1018, 'n_words': 330, 'path': '04_scope.ipynb', 'repo_name': 'imxw/GolangTraining', 'size': '2841'}