Sizeof struct in Go

Roger already showed how to use SizeOf method from the unsafe package. Make sure you read this before relying on the value returned by the function: The size does not include any memory possibly referenced by x. For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size … Read more

nested struct initialization literals

While initialization the anonymous struct is only known under its type name (in your case A). The members and functions associated with the struct are only exported to the outside after the instance exists. You have to supply a valid instance of A to initialize MemberA: b := B { A: A{MemberA: “test1”}, MemberB: “test2”, … Read more

How to check for an empty struct?

You can use == to compare with a zero value composite literal because all fields in Session are comparable: if (Session{}) == session { fmt.Println(“is zero value”) } playground example Because of a parsing ambiguity, parentheses are required around the composite literal in the if condition. The use of == above applies to structs where … Read more

Swift and mutating struct

The mutability attribute is marked on a storage (constant or variable), not a type. You can think struct has two modes: mutable and immutable. If you assign a struct value to an immutable storage (we call it let or constant in Swift) the value becomes immutable mode, and you cannot change any state in the … Read more

What lifetimes do I use to create Rust structs that reference each other cyclically?

It is not possible to create cyclic structures with borrowed pointers. There is not any good way of achieving cyclic data structures at present; the only real solutions are: Use reference counting with Rc<T> with a cyclic structure with Rc::new and Rc:downgrade. Read the rc module documentation and be careful to not create cyclic structures … Read more

Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

is there a way to do it without extracting their “inner” values every time with pattern matching, and without implementing the Add, Sub, … traits and overloading operators? No, the only way is to implement the traits manually. Rust doesn’t have an equivalent to the Haskell’s GHC extension GeneralizedNewtypeDeriving which allows deriving on wrapper types … Read more

Missing type in composite literal

The assignability rules are forgiving for anonymous types which leads to another possibility where you can retain the original definition of A while allowing short composite literals of that type to be written. If you really insist on an anonymous type for the B field, I would probably write something like: package main import “fmt” … Read more