Skip to content

Instantly share code, notes, and snippets.

@rominirani
Created June 30, 2015 12:02
Show Gist options
  • Save rominirani/99b23b4e90fa7aee30df to your computer and use it in GitHub Desktop.
Save rominirani/99b23b4e90fa7aee30df to your computer and use it in GitHub Desktop.
Week 2 Exercise Solutions from Go Course (http://gocourse.slack.com)
Exercise 11 : Line #5 and Line #6 have errors since you cannot have a pointer to a constant value.
Exercise 12 : The pointer is not initialized to anything (nil). As a result,any operation on this is going to be problematic (Null Pointer Exception). On running the program, it shows panic: runtime error: invalid memory address or nil pointer dereference.
Exercise 13 : a() is invoked first and this prints the value of the global variable i (10), which was initialized to 10 at the start of the program. The next function invoked is b() and this initializes a local variable i to 20. Hence the output is 20. This is a local variable within the scope of the function and does not affect the global variable i, which still has the value of 10. Hence the last function a() invocation simply prints the value of the global variable i that is still 10.
Exercise 14 : main() creates a localized variable i and hence the first output is 20. Then b() is invoked, which also creates a localized variable i, whose value is 40. And then finally a() is invoked which refers to the global variable i. This global variable was declared and hence it was initialized to a default int value of 0. Hence 20,40,0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment