Skip to content

Instantly share code, notes, and snippets.

@erikcorry
Created November 3, 2021 10:28
Show Gist options
  • Save erikcorry/00e11f7f46ff46d3141c2a0e001d9b16 to your computer and use it in GitHub Desktop.
Save erikcorry/00e11f7f46ff46d3141c2a0e001d9b16 to your computer and use it in GitHub Desktop.
package main
func main() {
// Two new vars, with :=.
x, y := returns_one_and_two()
print("x = ", x, "\n") // 1
print("y = ", y, "\n") // 2
// One of the vars is new, use := for both.
y, z := returns_one_and_two()
print("y = ", y, "\n") // 1
print("z = ", z, "\n") // 2
for i := 0; i < 2; i++ {
print("outer z = ", z, "\n") // 2
// One of the vars is new, use := for both.
z, w := returns_one_and_two()
print("inner z = ", z, "\n") // 1
print("w = ", w, "\n") // 2
}
// Now you expect z to be 1, but that was the inner z. This
// is the outer z, which is still 2.
print("outer z = ", z, "\n") // 2
}
func returns_one_and_two() (int, int) {
return 1, 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment