cjrd / allocate Goto Github PK
View Code? Open in Web Editor NEWEasily initialize golang structs with pointers to zero'd values instead of nil values
License: MIT License
Easily initialize golang structs with pointers to zero'd values instead of nil values
License: MIT License
When attempting to use the Zero method, I'm getting: Input interface is not addressable (can't Set the memory address)
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)}
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
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:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.