Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to fix issues caused by reusing Go error variables

October 17, 2022
Massimiliano Ziccardi
Related topics:
Go
Related products:
Red Hat Enterprise Linux

    This article discusses issues caused by reusing error variables in Go code. I will begin by illustrating the problem with a fictional company called ACME Corporation and its software engineer Wile E. Then I will present trivial examples to reveal the problem and how to avoid it.

    The story of a reused Go error variable

    A user opened a new issue to ACME Corporation because he noticed that users could enter any string into the phone number on the contact form. Wile E. is in charge of implementing a fix.

    Wile E. examines the code:

    func StoreAllContacts() {
      for _, c := range contactsToStore {
        err := legacy.StoreContact(&c)
        if err != nil {
          fmt.Printf("Error storing %v: %v\n", c, err)
        } else {
          fmt.Printf("Contact NAME: %s, SURNAME: %s, PHONE: %s stored successfully!\n", c.Name, c.Surname, c.PhoneNumber)
        }
      }
    }

    When he runs the code, he sees the following output:

    Contact NAME: John, SURNAME: Doe, PHONE: (012) 3456789 stored successfully!
    Contact NAME: Jane, SURNAME: Doe, PHONE: (089) 3456789 stored successfully!
    Contact NAME: Tom, SURNAME: Doe, PHONE: (083) 1234567 stored successfully!

    Wile thinks, "OK. Fixing the code should be easy. I will add the check for the phone number format just before calling the StoreContact function."

    Then, Wile produces the following code:

    const phoneNumberRe = `\(\d{3}\) \d{7}`
    
    func validatePhoneNumber(phoneNumber string) (bool, error) {
      re, err := regexp.Compile(phoneNumberRe)
      if err != nil {
        return false, err
      }
      return re.MatchString(phoneNumber), nil
    }
    
    func StoreAllContacts() {
      for _, c := range contactsToStore {
        valid, err := validatePhoneNumber(c.PhoneNumber)
        if err != nil {
          fmt.Printf("Invalid phone number RE ('%s'). Validation ignored.\n", phoneNumberRe)
        }
    
        if !valid {
          fmt.Printf("Invalid phone number '%s' for '%s %s'", c.PhoneNumber, c.Name, c.Surname)
        }
    
        err = legacy.StoreContact(&c)
        if err != nil {
          fmt.Printf("Error storing %v: %v\n", c, err)
        }
      }
    }

    Wile runs the code, but the output is not what he expected:

    Error storing {John Doe (012) 3456789}: <nil>
    Error storing {Jane Doe (089) 3456789}: <nil>
    Error storing {Tom Doe (083) 1234567}: <nil>

    Wile wonders what is happening: "The program enters the if err != nil branch, but then the output of the fmt.Printf shows that err is nil..."

    What makes Wile even more confused is that he didn't change anything inside StoreContact. So he wonders why the behavior changed.

    After some debugging, Wile decides to look at the StoreContact method:

    func StoreContact(c *Contact) *ContactError {
      if err := c.validate(); err != nil {
        return err
      }
    
      // ... store the contact somewhere
      return nil
    }

    Wile: "Oh! There it is!"

    Wile gets permission to fix the function as follows:

    func StoreContact(c *Contact) error {
      if err := c.validate(); err != nil {
        return err
      }
    
      // ... store the contact somewhere
      return nil
    }
    

    When he reruns the code, it finally works as expected. But how did he arrive at this fix?

    The fix explained: The hidden dynamic type

    To understand the issue, let's look at the following code:

    package main
    
    import "fmt"
    
    type MyTestError struct {
    }
    
    func (MyTestError) Error() string {
      return "Test Error String"
    }
    
    func main() {
      var err error // (1)
      fmt.Println("Is err nil?", err == nil)
      var mytesterror *MyTestError // (2)
      fmt.Println("Is mytesterror nil?", mytesterror == nil)
    
      err = mytesterror // (3)
      fmt.Println("Is err nil?", err == nil)
    }
    

    Here are descriptions of the commented-out numbers:

    1. We create a variable of type error. We didn't assign any value to it, so it will be nil.
    2. We create a variable of type *MyTestError. As before, we didn't give any value to it, so it will be nil.
    3. We assign mytesterror (nil) to err. Since mytesterror is nil, should be nil too.

    Let's try to run the code (you can run it at the Go Playground):

    Is err nil? true
    Is mytesterror nil? true
    Is err nil? false

    The reason for this unexpected behavior is that the interface type in Go is composed of a value and a dynamic type. The interface is considered nil only when the value and the type are nil.

    With that in mind, let's look again at the code:

    1. We create a variable of type error. We didn't assign any value to it, so it will be nil.
    2. We create a variable of type *MyTestError. As before, we didn't give any value to it, so it will be nil.
    3. We assign mytesterror (that is, nil) to err. After this assignment, the value of the err interface is nil, but the type is *MyTestError. As explained, an interface with a non-nil type is not nil even if the value is nil.

    To make the issue more evident, let's run the following code:

    package main
    
    import "fmt"
    
    type MyTestError struct {
    }
    
    func (MyTestError) Error() string {
      return "Test Error String"
    }
    
    func main() {
      var err error // (1)
      fmt.Println("Type of err:", reflect.TypeOf(err))
      fmt.Println("Is err nil?", err == nil)
    
      var mytesterror *MyTestError // (2)
      fmt.Println("Type of mytesterror:", reflect.TypeOf(mytesterror))
      fmt.Println("Is mytesterror nil?", mytesterror == nil)
    
      err = mytesterror // (3)
      fmt.Println("Type of err:", reflect.TypeOf(err))
      fmt.Println("Is err nil?", err == nil)
    }

    The output is:

    Type of err: <nil>
    Is err nil? true
    Type of mytesterror: *main.MyTestError
    Is mytesterror nil? true
    Type of err: *main.MyTestError
    Is err nil? false
    

    The assignment changed the type of err from <nil> to *main.MyTestError.

    The same happens with code like this:

    package main
    
    import (
    	"fmt"
    	"reflect"
    )
    
    type MyTestError struct {
    }
    
    func (MyTestError) Error() string {
    	return "Test Error String"
    }
    
    func MyTestFunc() error {
    	var err *MyTestError
    	return err
    }
    
    func main() {
    	err := MyTestFunc()
    	if err != nil {
    		fmt.Println("An error has occurred: ", err)
    		fmt.Println("Type of err: ", reflect.TypeOf(err))
    		return
    	}
    	fmt.Println("Success!")
    }

    If you try to run it in Go Playground, you will get the following output:

    An error has occurred:  <nil>
    Type of err:  *main.MyTestError

    The reason why this happens should now be apparent by now: to return the error, Go copied the value of err (of type *MyTestError) to a variable of type error: exactly as we did in the previous example.
    We can quickly fix the code returning a naked nil:

    package main
    
    import (
    	"fmt"
    	"reflect"
    )
    
    type MyTestError struct {
    }
    
    func (MyTestError) Error() string {
    	return "Test Error String"
    }
    
    func MyTestFunc() error {
    	var err *MyTestError
    	err = err  // let's do something with the variable to avoid compiling error
    	return nil
    }
    
    func main() {
    	err := MyTestFunc()
    	if err != nil {
    		fmt.Println("An error has occurred: ", err)
    		fmt.Println("Type of err: ", reflect.TypeOf(err))
    		return
    	}
    	fmt.Println("Success!")
    }

    Running the code in the Go Playground we now get the following output:

    Success!

    Three cases to keep in mind

    Reusing error variables and returning custom error structures is a widespread practice, and there isn't anything wrong with that. However, you should keep the form of the returned error in mind when checking it for a nil value:

    1. If you control the source code of the called function, ensure it returns an error interface and always returns a naked nil value.

    2. If the function you call returns a concrete object, use a dedicated variable to store that function error.

    3. In all the other cases, if you see weird behaviour where Go says that a nil variable is not nil, you can always use the reflect package to check the value specifically:

      reflect.ValueOf(err).IsNil()

    Summary

    I hope this has expanded your knowledge of the Go language. Please leave a comment below if you have questions about this article. As always, we welcome your feedback.

    Related Posts

    • Using Delve to debug Go programs on Red Hat Enterprise Linux

    • Troubleshooting and FAQ: Red Hat Enterprise Linux

    • Build your own RPM package with a sample Go program

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    What’s up next?

    Path to GitOps cover card

    Read The Path to GitOps for a comprehensive look at the tools, workflows, and structures teams need to have in place in order to enable a complete GitOps workflow.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.