golang – some constructs part 1
Since starting to play with golang I’ve run into a couple of interesting items I thought worth writing about. For those of you that are seasoned developers, I assure you, this wont be interesting. But for us that are getting started this might be worth reading.
Pointers
Nothing super exciting here if you’ve used them in other languages but it’s worth talking about since it can be confusing. Pointers are really just a way for us to gain access to the ‘real’ variable when you aren’t in the function that defines it. Put another way, when you call a function that takes a variable, you are only giving that function a copy of the variable, not the real variable. Pointers allow us to reference the actual location in memory where the value is stored rather than the value itself. Examples always make this more clear. Take for instance this example of code…
package main import "fmt" func main() { //Define myname and set it to 'jonlangemak' myname := "jonlangemak" //Rename without using pointers rename(myname) fmt.Println(myname) //Rename using pointers pointerrename(&myname) fmt.Println(myname) } //Function without pointers func rename(myname string) { myname = Continue reading