Null Pointer Challenge in Golang

package main

import "fmt"

type X interface {
X() string
}
func greeting(thing X) string {
return "Hello " + thing.X()
}

func main() {

fmt.Println("nil pointer dereference")
greeting(nil)
}
type myNumber struct {
n int
}
func addOne(number *myNumber) {
number.n++
}
func main() {        var number *myNumber        addOne(number)    }

Solution for nil pointer deference

func sample(x Named) (string, error) {
if x == nil {
return "", errors.New("x cannot be nil")
}
return "... " + x.Name(), nil
}

--

--

--

ByteScan is a Cyber Security Consulting Firm that offers security auditing services for a number of blockchains, with a special focus on third-generation dapps.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Best practices for building effective, clutter-free, and scalable Confluence content: Part 2

What Is Big-O-Notation?

CS373 Spring 2021 Blog #9: Regina Chen

DANGEROUS Sidewalks in Santiago!

FastAPI — Google as an external authentication provider

Experiential Learning Theory and Coding

Stop Waiting Until Things are Perfect

6 Inspiring Cross-Platform App Development Trends In 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
bytescan

bytescan

ByteScan is a Cyber Security Consulting Firm that offers security auditing services for a number of blockchains, with a special focus on third-generation dapps.

More from Medium

Writing a simple WebApi with Golang and MsSql

How to Check nil Interface in Golang?

Go Vanity Imports

lancet (util function library of go) released v2.0.0, fully supports go generic features