Skip to content
/ opt Public

convenience functions for dealing with struct fields that are pointers to primitives

Notifications You must be signed in to change notification settings

dtcookie/opt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opt

This module is supposed to ease up access to golang struct fields that are pointers to primitives - with the premise that nil pointers are equivalent to the zero value of said primitive.

The convenience functions offered by this module essentially abstract away the obligatory nil checks when accessing such fields, thereby fostering easier to read code. Because the go compiler cannot produce pointers to primitive literals, the same helper functions exist for creating pointers to initialized primitive values using a one liner.

package main

import "github.com/dtcookie/opt"

type Record struct {
    Value *int
}

func main() {
    // record.Value uninitialized
    record := new(Record)

    // manual nil check
    var a int
    if record.Value != nil {
        a = *record.Value
    }

    // nil check performed by opt module
    b := opt.Int(record.Value)

    // assignment using a helper variable
    // the compiler won't accept `record.Value = &3`
    c := 3
    record.Value = &c

    // helper variable abstracted away by opt module
    record.Value = opt.NewInt(3)

}

About

convenience functions for dealing with struct fields that are pointers to primitives

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages