Composite-literals are a way of declaring and allocating resources in the Go programming language.
Questions tagged [composite-literals]
30 questions
30
votes
3 answers
Golang embedded struct type
I have these types:
type Value interface{}
type NamedValue struct {
Name string
Value Value
}
type ErrorValue struct {
NamedValue
Error error
}
I can use v := NamedValue{Name: "fine", Value: 33}, but I am not able to use e :=…

Ayman
- 11,265
- 16
- 66
- 92
12
votes
7 answers
Prevent missing fields in struct initialization
Consider this example. Let's say I have this object which is ubiquitous throughout my codebase:
type Person struct {
Name string
Age int
[some other fields]
}
Somewhere deep in the codebase, I also have some code that creates a new…

elijahcarrel
- 3,787
- 4
- 17
- 21
10
votes
2 answers
Understanding go composite literal
Why a function value assignment to f is not a composite literal?
Go lang specification Composite literals says below, hence function value cannot be constructed with composite Literal.
Composite literals construct values for structs, arrays,…

mon
- 18,789
- 22
- 112
- 205
10
votes
1 answer
Struct in for loop initializer
Any idea why this struct expression in for loop initializer makes syntax error in compile-time? Pointer to struct works fine in this case but ofc I need local variable like bellow. Thanks for advices!
type Request struct {
id int
line…

bigless
- 2,849
- 19
- 31
9
votes
2 answers
Golang struct literal syntax with unexported fields
I've got a largish struct which until just now I was instantiating with the struct literal syntax, e.g.:
Thing{
"the name",
...
}
I've just added an unexported field the Thing struct and now Go is complaining: implicit assignment of unexported…

jbrown
- 7,518
- 16
- 69
- 117
8
votes
2 answers
Go build with protocol buffer error: too few values in struct initializer
I have a proto file:
syntax = "proto3";
package main;
message Client {
int32 Id = 1;
string Name = 2;
string Email = 3;
}
The compiled Client struct like below:
type Client struct {
Id int32 …

yong ho
- 3,892
- 9
- 40
- 81
8
votes
1 answer
Anonymous struct, difference between struct{}{} and {}
I have string to struct map in golang defined in the following way:
var Foo = map[string]struct{}{
"foo": struct{}{},
}
Gogland by default marks this declaration as warning, saying "Redundant type declaration".
var Foo = map[string]struct{}{
…

hywak
- 890
- 1
- 14
- 27
6
votes
2 answers
How to literally initialize multi-level nested structs in GO?
I am trying to literally initialize the following struct in GO:
This is the struct:
type tokenRequest struct {
auth struct {
identity struct {
methods []string
password struct {
user struct {
…

geexee
- 339
- 2
- 13
5
votes
1 answer
What does initializing a Go struct in parentheses do?
Normally, I will initialize a struct like:
item1 := Item{1, "Foo"}
However, I've recently seen code initializing with parens:
item2 := (Item{2, "Bar"})
reflect returns the same Item name.
What does initializing in parentheses do and when is it…

Grokify
- 15,092
- 6
- 60
- 81
5
votes
3 answers
Go struct literals, why is this one addressable?
I am reading the book "The Go Programming Language". It is very good for us (rather) experienced programmers and explains the differences between the intersection of other languages -- but I've found a case which I do not understand fully.
I know…
user1781434
3
votes
3 answers
Extend map from other packages at compile time
I'm trying to extend a map across packages at 'compile time'. Is this possible?
I have package A with a predefined map:
package A
var MyMap = map[string]string{"key1": "value", "key2": "value"}
And I would like to extend the map during 'compile…

xsigndll
- 513
- 1
- 5
- 13
2
votes
2 answers
Properly initialize a map[string]interface struct
I have the following struct:
type InstructionSet struct {
Inst map[string]interface{}
}
In the Inst map I'd like to put something like
Inst["cmd"] = "dir"
Inst["timeout"] = 10
Now I'd like to initialize it directly from code, but I'm not…

cips
- 95
- 1
- 8
2
votes
1 answer
Does golang allocate new memory when reassign a new struct to a variable?
When I reassign a new struct object to an existing variable, the address doesn't change. Code is shown below:
type Request struct {
Field string
}
func main(){
r := Request{Field: "a"}
fmt.Printf("%p\n", &r)
r = Request{Field: "b"}
…

Alexander
- 523
- 5
- 21
2
votes
2 answers
How to declare nullable json fields in golang structs?
I usually like to use pointers for primitive data types in my structs so that when I json.Marshal them, the nil fields are always translated to "field": null in the json string. But this will make it difficult to create a new struct instance since I…

Kashif Minhaj
- 83
- 7
2
votes
3 answers
Slice of structs behaviour
When we declare a slice of structs, but set the data type to a pointer to the struct, does go automatically convert the literals to pointers?
type book struct {
id int
name string
}
var books = []*book {
{1, "Yellow moon"}, // ---> Is…

Shubhang b
- 597
- 1
- 5
- 12