Contact Information and Pointer Pointers

Contact Information and Pointer Pointers

Site Link
Twitter @krisnova
LinkedIn  kris-nova
GitHub  kris-nova

My contact information. This will make more sense later.

 

Like all good blog posts. This too started out as a tweet.

 

 

 

The conversation was regarding me changing my Twitter handle, and Justin was bringing up a great point. Which is that my Twitter handle is hard-coded all over the internet. My engineer brain instantly started to draft up solutions to this problem, as I realized this was the perfect use case for pointer pointers!

Pointers in Go

Now if you are new to pointers in Go don’t worry. Pointers are easy.

Just think of pointers as URLs!

Seriously, a URL is just a pointer to a web page. In this case the URL itself (say https://nivenly.com) is the address and whatever the web page returns is it’s value.

See? Pretty easy right. I told you pointers were no big deal.

Pointer Pointers

So in the case of changing my contact information, let’s take a look at how things are today.

Notice how in this model, if I need to revise my contact information I would need to go an update every page on the internet that has my contact information linked to it. If we had a pointer page we could safely hard code all the pages on the internet to one page that I control. Say a page on my blog for instance. Then if we needed to update anything all we need to do is update the pointer page.

 

In this model, we no longer need to make a change to the web page on the internet as it will also be pointing to the pointer page.

An example in Go

 

Below is a concrete example of pointer pointers in Go, using the use case from the tweet. Needing to change your twitter URL without changing the higher level implementation.

 


package main

import (
	"fmt"
)

type ContactInfo struct {

	Twitter string
	LinkedIn string
	GitHub string

}

func main() {
	
	// -----------------------------------------------------
	// First version of my contact information
	kris := &ContactInfo{
		Twitter: "https://twitter.com/kris__nova",
		LinkedIn: "https://linkedin.com/in/kris-nova/",
		GitHub: "https://github.com/kris-nova",
	}


	// -----------------------------------------------------
	// Assign my contact information as a pointer pointer
	myContactInfo := &kris
	fmt.Println(**myContactInfo)
	
	
	// -----------------------------------------------------
	// Second version of my contact information
	kris = &ContactInfo{
		Twitter: "https://twitter.com/krisnova",
		LinkedIn: "https://linkedin.com/in/kris-nova/",
		GitHub: "https://github.com/kris-nova",
	}
	
	
	
	// -----------------------------------------------------
	// Feel free to hard code myContactInfo anywhere and I 
	// can change it right from underneath you!
	fmt.Println(**myContactInfo)
}

Run it on the Go playground!

 

 

Cheersthanks!

 

 

 

LEAVE A COMMENT

0 comment