GithubHelp home page GithubHelp logo

cjrd / allocate Goto Github PK

View Code? Open in Web Editor NEW
24.0 3.0 4.0 18 KB

Easily initialize golang structs with pointers to zero'd values instead of nil values

License: MIT License

Go 100.00%
golang-structs golang-reflection golang

allocate's Issues

Can't work with unexported fields in protobuf

Protobuf now has 3 unexported fields

type MyPbStruct struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	MyInt int32 `protobuf:"varint,1,opt,name=my_int,json=friendRuid,proto3" json:"my_int,omitempty"`
}

When trying to set this struct zero

st := new(MyPbStruct)
err := allocate.Zero(st)
if err != nil{
	log.Error(err)
}

I got

struct field can't interface, &impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(nil)}

Example code in readme is wrong

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

    // try to allocate.Zero a pointer to pointer
    // should be: allocate.Zero(topStruct)
    allocate.Zero(&topStruct)
    fmt.Printf("post allocate.Zero: %v\n", topStruct)
    fmt.Printf("topStruct.MyEmbeddedStruct.SomeInt==%d\n", topStruct.MyEmbeddedStruct.SomeInt)
}

A panic occurs when there is a circular reference to the structure

A panic occurs when there is a circular reference to the structure

code

func Test_xxx(t *testing.T) {
	type Node struct {
		ID   int
		Next *Node
	}
	var node Node
	err := allocate.Zero(&node)
	if err != nil {
		panic(err)
	}
	t.Logf("%+v", node.Next)
}

run test

runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0xc0200f0388 stack=[0xc0200f0000, 0xc0400f0000]
fatal error: stack overflow

runtime stack:

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.