How to Perform Null Checks for Structs in Golang?

In this quick guide, you'll learn to deal with comparing struct objects containing referential composite properties like slices, maps or channels etc.

Share:

Feb 3, 2024 391 Words

Read Time: 2 Minutes

In this quick guide, you'll learn to deal with comparing struct objects containing referential composite properties like slices, maps or channels etc.

In this quick guide, you’ll learn to deal with comparing struct objects containing referential composite properties like slices, maps or channels etc.

If I want to check if a struct object is empty, my initial thought would have been to simple check it like this


var oObj = A{}

if (aObj == A{}) {
// ... handle null case
} else {
// ... handle non-null case
}

But sometimes, we get an error like this,

invalid operation: aObj == A{} (struct containing B cannot be compared)

why?

let’s take a look at the actual struct definition.


// ...

type A struct {
	a uint
	b B
}

type B struct {
	ok bool
	values []string
}

// ...

Yes. The issue lies in the []string (slice) composite datatype as a property of struct B.

The == based equality check between struct objects only holds if the struct is a pure struct (or) a select few composite datatypes.

A pure struct has basic datatype like int, string, float64, bool etc.

If any property is of type slice, map or another struct containing said composite types, then the above code will not run (atleast the code won’t compile).

So what now?

We can configure a method called, let’s say, isEmpty for a struct, that returns true if the nested composite type’s null value is checked.

I can define a isEmpty like method on the struct A and B, to finally check the null value of the composite type that is hindering the basic comparison operation.

If the nested struct(s) still aren’t pure enough, then we do the same for that so we check for their isEmpty and so on until we find the composite types affecting the basic comparison operation.

// ...

func (aObj A) isEmpty() bool {
	return aObj.b.isEmpty()
}

func (bObj B) isEmpty() bool {
	return bObj.values == nil  // Assuming if 'values' is empty, then object can be considered empty.
}

// ...

so now, the the implementation would go like this,

// ...

var aObj = A{}

if aObj.isEmpty() {
// ... handle null case
} else {
// ... handle non-null case
}

// ...

If the nested layers goes deep, then it might be the right time to rethink the design but that depends on the use-case.

I hope you found this useful.

👋 — @TnvMadhav

Find more posts from following topics

accurate-requests
api-development
api-testing
api-testing-tools
array
automated-testing
bad-habits
base64-decoder
base64-encoder
binding
blog
blogging
bulma-css
bulma.io
button-swiftui
chatgpt
clipboard
code
code-block
code-snippet
comparison
compile
configuring-debugger-for-django-in-vs-code
configuring-launch.json-for-python-debugger
copy
copy-to-clipboard
copy-to-clipboard-neovim
css
current-date
current-time
current-timestamp
debugger-setup-in-visual-studio-code
debugging-django-app-in-visual-studio-code
debugging-python-code-in-visual-studio-code
debugging-python-programs-with-visual-studio-code
debugging-python-with-virtual-environment-in-vs-code
developer-productivity
developers
development-workflow
dom
dynamic-sitemap-in-nextjs
engineering-dashboard
flowcharts
git
git-diff
github
global-keyboard-shorcut
global-shortcut
go
go-hugo
go-programming
go-to-line
golang
golang-development
good-habits
gorilla-websocket
gpt
gpt-3.5
gpt-4
gpt-4-api
guide
gumroad
habits
habits-tracker-notion-template
hamburger-menu
hotkeys
html
hugo
ide
image
image-sharing
image-tool-for-ios
imagerenderer
include-timestamp
integrated-development-environment
ios
ios-16
ios16
javascript
keyboard-shortcut
linux
macos
map
markdown
markdown-code
mental-programming
menu
menubarextra
mermaid-syntax
mistake-tracker-notion
mobile-view
modifier
navbar
navigationlink
navigationstack
neovim
next.js
nextjs
nextjs-markdown
nextjs-sitemap
nextjs-sitemaps
nice-shot
nice-shot-pro
notion
notion-api
notion-api-python
notion-budget
notion-budget-template
notion-budget-tracker
notion-bug-report-tracker
notion-dashboard
notion-expense-manager
notion-habits
notion-habits-dashboard
notion-habits-template
notion-habits-tracker
notion-habits-tracker-template
notion-issue-tracker
notion-mistake-tracker
notion-product
notion-product-dashboard
notion-product-roadmap
notion-product-roadmap-dashboard
notion-tasks
notion-tasks-dashboard
notion-tasks-template
notion-tasks-tracker
notion-template
notionworkspaces
openai
osx
personal-ifttt-framework
photospicker
photospickeritem
phpickerfilter
postman-capabilities
postman-request
pre-request-script
product-roadmap-notion-template
product-roadmap-template
productivity
programming
python
python-api
python-debugger-tutorial-for-vs-code
python-debugging-mode-in-vs-code
python-notion-api
real-time-communication
rehype
remark
request-data
running-debugger-in-visual-studio-code
running-django-app-in-debugging-mode
running-program-in-debugging-mode-in-vs-code
running-python-code-in-debugging-mode
screenshot-app-for-ios
screenshot-app-ios
screenshot-ios
screenshot-tool-for-ios
set-current-timestamp
setting-up-debugger-in-vs-code-for-python
share-extension
sharelink
sharepreview
sharesheet
simple-websocket-server
sitemap
slice
slider
step-by-step-guide
stocks-profits-tracker
stocks-profits-tracker-template
stocks-tracker
struct
sustained-vigilance
swift
swiftui
swiftui-button
swiftui-button-action
swiftui-button-style
table-of-contents
tasks-tracker-notion-template
textfield-swiftui
timeliness
timestamp-integration
transferable
triggers-and-actions
tutorial
us-stocks
usa-stocks
useful-ios-features
using-breakpoints-in-python-debugger
using-virtual-environment-with-python-debugger
vanilla-javascript
variable
vim
visual-mode
visual-studio-code
vs-code
vscode
vscode-go-to-line
web-sockets-in-go
websocket-client
websocket-programming
websocket-server
xcode