Use optinal type-like in Go language


I'm using the go language and I'm having trouble using optional. Since it is not enough to include the library, I wrote it with reference to the following code. Reference

Overview of implemented method

NewInt(i int) optional.Int Create an optional type. Get() (int, bool) Take out the contents of optinal. Returns value true if the value exists, 0, false if not. Set(i int) Set the value to optional. Present() bool Check if there is a value.

Code contents

package optional

// Int - optional int
type Int struct {
	value *int

// NewInt - create an optional.Int from int
func NewInt(i int) Int {
	return Int{value: &i}

// Get - get the wrapped value
func (i Int) Get() (int, bool) {
	if i.Present() {
		return *i.value, true
	var zero int
	return zero, false

// Set - set value
func (i Int) Set(j int) {
	i.value = &j

// Present - return whether or not the value is present.
func (i Int) Present() bool {
	return i.value != nil


It's a simple implementation, but I'm glad I could do something like optional. It is good to be able to write the state of None with optional.Int {}. If you want to decode to json or want optional in other types, you can import

Recommended Posts

Use optinal type-like in Go language
Hello World in GO language
Post to slack in Go language
Write tests in GO language + gin
Do something object-oriented in GO language
use go module
Java programmer touched Go language (Implement Java inheritance in Go language)
I wrote the hexagonal architecture in go language
Create a web server in Go language (net/http) (2)
Write Pulumi in Go
Use config.ini in Python
Use DataFrame in Java
Use dates in Python
Go language learning record
Go language environment construction
Use Mean in DataFrame
Use Valgrind in Python
Use ujson in requests
Go language cheat sheet
Features of Go language
Create a web server in Go language (net / http) (1)
Use profiler in Python
Go language to see and remember Part 7 C language in GO language
[Go language] How to get terminal input in real time
Machine language embedding in C language
Use "$ in" operator with mongo-go-driver
Use let expression in Python
Heapsort made in C language
Use Anaconda in pyenv environment
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use regular expressions in C
Implement recursive closures in Go
Use MongoDB ODM in Python
Use list-keyed dict in Python
Programming language in "Hello World"
Use Random Forest in Python
Use regular expressions in Python
go language learning miscellaneous notes 1
Use Spyder in Python IDE
[Golang] About Go language channel
[Go] How to use "... (3 periods)"
Use Juman ++ in server mode
ยท Address already in use solution
Created a package to support AWS Lambda development in Go language
Created gomi, a trash can tool for rm in Go language
Read the config file in Go language! Introducing a simple sample
Think seriously about what language to use in programming education and programming education.