Back To Blog

Pointers in GO

One of the hardest things I found in trying to learn Go – this before I got to Concurrency and Go Routines – was the idea of Pointers. I understood that they point to location of a variable, rather than its value. What I didn’t understand was – why are pointers useful? So what – you can point to a variable’s location rather than its value – but how does that help in any practical way and why does it figure so prominently in every tutorial on Go?

I came to GO from Javascript, PHP and Python, none of which explicitly use Pointers (though as we’ll see below, Python does use a form of Pointers).

Pointers are relatively easy to create and use in Go. The syntax we use to define a pointer is:

var pointerName *dataType

The pointerName is the name of the variable that will hold our pointer. The dataType is the type that we associate with the value stored in the pointer. Here is a simple example of creating a pointer in the default main.go file:

package main
import "fmt"
func main() {
     var a = 10 // variable a stores value of 10
     var b *int = &a // variable b stores the memory address (location) but NOT the value of variable a
}
     fmt.Println(a) // prints '10'// our value
     fmt.Println(b) // prints '0xc0000aa058', the location of our variable

So simple enough to get my head around, but I still didn’t understand why pointers were such a key part of the Go language. To understand their practical applications, I asked, as one does, the LLMs, specifically Gemini Pro, which I’ve been using more and more lately. Some of its response was reproduced below:


Here are the three practical reasons why we use pointers in Go.

1. Mutability (Changing the Original)

Because Go copies everything by default, a function cannot change the state of a variable passed to it unless we give it the “address” (pointer) of that variable.

The “Oops” moment (Pass by Value):

Go

func changeName(name string) {
    name = "Alice" // This only changes the COPY inside this function
}
func main() {
    user := "Bob"
    changeName(user)
    fmt.Println(user) // Output: Bob (Nothing happened!)
}

The Fix (Pass by Pointer):

Go

// We accept a pointer to a string (*string)
func changeName(name *string) {
    *name = "Alice" // We travel to the address and update the value there
}
func main() {
    user := "Bob"
    changeName(&user) // We pass the address (&)
    fmt.Println(user) // Output: Alice
}

Practical Application: We see this everywhere in Go methods: if a method needs to update the state of a struct (like setting a User.IsLoggedIn = true), it must use a pointer receiver.


2. Performance (Efficiency)

Imagine we have a struct that represents a massive JPEG image or a user profile with 50 fields.

  • Without Pointers: Every time we pass that struct to a function, Go has to pause and copy every single bit of that data into a new location. This eats up memory and CPU.
  • With Pointers: We pass the memory address. An address is tiny (usually 8 bytes on a 64-bit system).

Python, by contrast, does this for us automatically. It never copies the whole image object; it just passes the reference. In Go, you have to choose to do this manually by using a pointer.

Rule of Thumb: If the data is small (like an integer or a coordinate point), copy it. If it is large (like a User struct or a config object), use a pointer to share it without copying.


3. Representing “Null” (Absence of Value)

This is a massive one for web development (SQL/JSON).

In Go, types have “zero values.”

  • The zero value of an int is 0.
  • The zero value of a string is "".

The Problem:

If a user submits a JSON form and the “age” field is 0, did they enter 0, or did they just leave it blank? In standard Go, we can’t tell.

The Solution:

Pointers can be nil.

  • var age int -> Default is 0.
  • var age *int -> Default is nil.

If age is nil, the user left it blank. If age points to a value of 0, then we know the user explicitly typed 0.

Practical Application: We see this quite a lot in database structs.

Go

type User struct {
    Name      string
    DeletedAt *time.Time // Pointer allows this to be null (user is active)
}

If DeletedAt wasn’t a pointer, it would default to the year 0001-01-01, which is confusing data.


Summary Table

FeaturePass by Value (No Pointer)Pass by Pointer (*)
AnalogySending a friend a photocopy of a map.Sending a friend the GPS coordinates.
MutabilityThey can draw on the map, but your map stays clean.If they change the location, it changes for you too.
PerformanceSlow for large items (requires photocopying).Fast (just sending coordinates).
SafetySafe. Original data cannot be corrupted.Risky. Original data can be modified or be nil.

A Note on Python

Python, it seems, does use pointers exclusively for objects (Lists, Dicts, Classes)—it just hides the syntax from you (* and &).

  • Python: a = [1, 2]; b = a (b is now a pointer to a).
  • Go: a := []int{1, 2}; b := a (b is a pointer to the underlying array).*

(Additional Note: Go Slices and Maps are special types that implicitly act like pointers, which helps make Go feel friendlier, but Structs do not.)


Back To Blog

A Gentle Guide to Chat GPT, THE ChatBot threatening to take over the world

Introduction: So What is Chat GPT?

Chat GPT is an AI model developed by OpenAI, which uses machine learning to generate human-like text based on a given input. The GPT stands for ‘Generative Pre-training Transformer’. It was released in late November 2022. Within a week, it had a million subscribers, within two months, according to the London Guardian, 100 million, making it the fastest growing app since apps were created.

Some have called it an auto-complete on steroids, but it is more than that because of its ability to remember ‘conversations’ (more on this later) and to ‘learn’. Chat GPT is a ‘Large Language Model’, trained on roughly 570GB units of data over a period of a year and a half. Chat GPT uses a specific type of deep learning called a transformer, which makes it really good at understanding the context of language, which makes it very good at generating human-like responses. It should be noted, though, that because it took a year and a half to train GPT, Chat GPT’s world stops a year and a half before it’s release – mid to late 2021, depending on the model.

Chat GPT also has a cousin, Bing, and an offspring, Chat GPT4. Both Chat GPT (which is v3.5) and Bing are free, with Bing bundled both as an app and as part of the Microsoft Edge browser. Bing is basically a search engine built on top of the GPT technology that powers Chat GPT. Chat GPT4 requires a $20/month fee, and features not just a (much) superior service, both in depth and accuracy of responses, and continual service (the free version sometimes goes down due to the sheer number of users), but also access to plugins and the ability to use Bing to browse the web in real time. The plugins allow Chat GPT to be extended in almost infinite directions – shopping, academia, travel, interacting with PDFs, data and stock analysis. Dozens of new plugins are being approved on the platform each week, with hundreds of apps and plugins being released outside the Open AI concurrently. As with any new technology – some are useful and . . . ,some are not.

Chat GPT shares some similarity to that other famous AI, IBM Watson, but some important differences as well. IBM Watson has been around for awhile, and has been used in many applications, from diagnosing medical conditions to answering customer service inquiries to customer service inquiries to employee training. Watson was originally designed to understand and answer questions in natural language, which it famously demonstrated by competing on the game show Jeopardy. Chat GPT, on the other hand, is focused on text generation. Chat GPT is all about creating human-like text on the input it’s given. So while they are both AI tools, they are different tools for different tasks.

Another important difference: using IBM Watson ain’t cheap, so its (admittedly impressive) suite of applications are typically used by businesses and organizations rather than individuals. Chat GPT, on the other hand, can be accessed by anyone with access to a web browser and an email account to start an account, thus making AI accessible to anyone on the planet(until, of course, Open AI decides to eliminate the free option. So enjoy it while it lasts.

But What does Chat GPT ACtually DO?

In terms of text, and everything to do with text, which is a good deal of our civilization, the better question might be: What does it NOT do? To answer this question, I asked Chat GPT(4) itself:

What can Chat GPT be used for? Please generate a list of 10 examples

  • Content Generation: ChatGPT can be used to draft articles, blog posts, or other forms of written content. This can be helpful to start a writing project or to get past writer’s block.
  • Creative Writing: It can generate creative text, such as stories, poems, or scripts. This can be a fun way to create unique narratives or to get inspiration for your own creative writing.
  • Brainstorming Ideas: ChatGPT can be used to generate ideas for a variety of purposes, from business plans to creative projects.
  • Customer Service: It can be used in chatbot interfaces to interact with customers, answer frequently asked questions, and provide information about products or services.
  • Tutoring: ChatGPT can be used to explain complex topics or concepts in a simpler, more understandable way.

The other 5 were Language Translation, Email Drafting, Coding Assistance, Role-Playing and Simulation, and Personal Assistant. Anyone reading this can ask Chat GPT themselves, with a simple prompt ‘What are you good for?’ and receive a reasonably thoughtful reply. I use GPT-4 to generate code, to generate ideas, problem-solving, learning plans, to explain mathematical or programming concepts I don’t understand, to generate a basic structure for a cover letter or, for that matter, this article. I’ve used it to fill out a plot point or something about a character in a novel, to find parallels in mythology or literature with an idea I’ve had. And I feel like i’ve barely scratched the surface of what it’s capable of.

One thing Chat GPT, 3.5 or 4, does NOT do well is write. It really sucks at writing and, moreover, unless we all want to live in Idiocracy in real time, it should suck at writing. Which of course hasn’t stopped thousands, millions of students generating their essays, papers, what have you in Chat GPT then running them through various tools to avoid other tools that detect AI generated content. Nor, alas, has it stopped would-be bloggers generating their content with AI, or even online sites, already heavily reliant on SEO keyword – laden articles, listicles – who have been using AI in one form or another to generate content for the last couple of years anyway – from using Chat GPT to generate content then having editors ‘curate’ that content before publication. This is one truly depressing (and dangerous) aspect of Chat GPT and AI generally (more on this anon).

https://chat.openai.com/share/cd5fb547-f375-45e9-afd9-9c3982407b32

HOW TO ACCESS Chat GPT

Accessing Chat GPT (the free version) is as simple as creating an account after following the link from the Open AI landing page and punching in an email and password. Bing can be found at bing.com or bundled as the default search engine with Microsoft’s Edge browser. From there, you ask questions of the model the way you would with a search engine, with the major difference that with Chat GPT and to some extent Bing, is that you’re not just querying a search request, but having a conversation.

Some things to remember

The most important thing to remember about Chat GPT and, to a somewhat lesser extent, Bing, is that they are both iterative, that, unlike a pure search engine like Google, they remember the questions and responses in a thread and thus have a human-like quality in the call and response. Since Bing has access to the web, it can return real-time data – and provide sources, which Chat GPT does not. It’s also important to remember that Chat GPT is not always accurate. Sometimes it ‘hallucinates’ – gives answers, often with an almost gleeful (like) confidence, when it doesn’t actually know the answer. GPT4 is much more reliable, but even GPT4 does produce questionable results from time to time.

This has been hailed as proof that Chat GPT doesn’t actually work, but I think critics miss the point, both about the software and how to use it. No software is bug-free when it’s first rolled out, and for Chat GPT to go from 100,000 to 100 million to something like a billion users in just a few months understandably put incredible strain on its creator. But more importantly, it shouldn’t really be thought of as software.

Two other very important considerations: security and timeliness.

Security

Everything you put on Chat GPT stays on Chat GPT, very likely to be sucked into the data maw that is its training base. One of the more interesting facets of using Chat GPT is that it’s always learning, and seems to learn to anticipate your needs and even personality to some extent. And one of the more disturbing aspects of Chat GPT is . . . it’s always learning and it’s learning from the data you, me and a billion of other users (as of this writing) have prompted their way into over the last six months of its public existence. So don’t put in personal details, or any other sensitive data. As soon as you do, it becomes public property and yet more training data for Chat GPT.

Timeliness

Chat GPT v3 was released in late November 2002, Chat GPT v4 in early February 2023 (the free version is now Chat GPT v3.5 – v3 has been discontinued). But it took a year and a half up to that date to train the AI model that is Chat GPT on basically the entire internet – and because of that, Chat GPT’s awareness of the world stops somewhere around late summer 2021. So don’t use Chat GPT for current events, or any knowledge released in the last year and a half; for that use Bing, which can access the internet in current time, or a regular search engine.

CHAT GPT: Your new personal Assistant

Another reason to learn how to use Chat GPT and more generally AI: its development is moving at light speed. Some have compared its importance, and potential impact, as high as that as the arrival of the web (Bill Gates has gone further, saying the development of AI was as “fundamental as the creation of the microprocessor, the personal computer, the internet, and the mobile phone.” But since his old company funds Open AI, he would say that wouldn’t he?). But the main reason I advocate learning how to use it is, unlike the web, this is moving fast. Will AI eliminate jobs? In my opinion, in the long term, almost certainly yes – but in the short term, you might not lose your job directly to AI, but you could very well lose your job to someone who knows how to use AI.

So learn how to use AI. Stay tuned for more . . . .