Giter VIP home page Giter VIP logo

allocate's Introduction

Allocate

Build Status Coverage Status Go Report Card MIT licensed GoDoc

Allocate provides functions for allocating golang structs so that pointer fields are pointers to zero'd values instead of nil. See the godoc's for more information: https://godoc.org/github.com/cjrd/allocate

Brief Example

package main

import (
    "fmt"
    "github.com/cjrd/allocate"
)

type TopLevelStruct struct {
    MyEmbeddedStruct *EmbeddedStruct
}

type EmbeddedStruct struct {
    SomeString string
    SomeInt int
}

func main() {
    topStruct := new(TopLevelStruct)
    fmt.Printf("before using allocate.Zero: %v\n", topStruct)

    allocate.Zero(&topStruct)
    fmt.Printf("post allocate.Zero: %v\n", topStruct)
    fmt.Printf("topStruct.MyEmbeddedStruct.SomeInt==%d\n", topStruct.MyEmbeddedStruct.SomeInt)
    // Note that panics would occur by executing `*topStruct.MyEmbeddedStruct` or
    //`topStruct.MyEmbeddedStruct.SomeInt`
}
# OUTPUT
before using allocate.Zero: &{<nil>}
post allocate.Zero: &{0x8201d2400}
topStruct.MyEmbeddedStruct.SomeInt == 0

Use Cases

  • Initializing structures that contain any type of pointer fields, including recursive struct fields
  • Preventing panics by ensuring that all fields of a struct are initialized
  • Initializing golang protobuf struct (the golang protobuf makes heavy use of pointers to embedded structs that contain pointers to embedded structs, ad infinitum)
  • Initializing structs for black box testing (see also https://golang.org/pkg/testing/quick/)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.