Is it possible to declare local anonymous structs in Rust?

While anonymous structs aren’t supported, you can scope them locally, to do almost exactly as you’ve described in the C version: fn main() { struct Example<‘a> { name: &’a str }; let obj = Example { name: “Simon” }; let obj2 = Example { name: “ideasman42” }; println!(“{}”, obj.name); // Simon println!(“{}”, obj2.name); // ideasman42 … 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

Struct’s zero value

Why guess (correctly) when there’s some documentation ? When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default … 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

Initialize embedded struct in Go

req := new(MyRequest) req.PathParams = pathParams req.Request = origRequest or… req := &MyRequest{ PathParams: pathParams Request: origRequest } See: http://golang.org/ref/spec#Struct_types for more about embedding and how the fields get named.

Error: struct Type is not an expression

The error is on this line var s = Salutation The thing to the right of the = must evaluate to a value. Salutation is a type, not value. Here are three ways to declare s: var s Salutation // variable declaration using a type var s = Salutation{} // variable declaration using a value … Read more

Import struct from another package and file golang

In Go you don’t import types or functions, you import packages (see Spec: Import declarations). An example import declaration: import “container/list” And by importing a package you get access to all of its exported identifiers and you can refer to them as packagename.Identifiername, for example: var mylist *list.List = list.New() // Or simply: l := … Read more