Questions tagged [composite-literals]

Composite-literals are a way of declaring and allocating resources in the Go programming language.

30 questions
1
vote
1 answer

golang: How to define a sequence of steps

I want to write a protocol test. My current idea is to define a series of steps. Each Step contains a Trigger and Expectation - e.g. trigger MsgA and expect MsgB. type Series struct { Steps []Step } What I don't like about this, is that the…
transient_loop
  • 5,984
  • 15
  • 58
  • 117
1
vote
1 answer

Go generics: invalid composite literal type T

package main import ( "google.golang.org/protobuf/proto" ) type NetMessage struct { Data []byte } type Route struct { } type AbstractParse interface { Parse(*NetMessage) proto.Message } type MessageParse[T proto.Message] struct…
lori227
  • 23
  • 1
  • 3
1
vote
1 answer

Go: Confusion about initialization of struct array

My Confusion is shown in the following code snippet: type Ex struct{ A,B int } a := []Ex{Ex{1, 2}, Ex{3, 4}} //it works, and I understand it b := []*Ex{&Ex{1, 2}, &Ex{3, 4}} //it works, and I understand it c := []Ex{{1, 2}, {3, 4}} //it…
xxllxx666
  • 183
  • 1
  • 6
1
vote
1 answer

How to remove the primitive.E composite literal uses unkeyed fields error?

In this code, I am trying to add a new field in the MongoDB database. But it is giving me a problem in the update variable and that is go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fields. I don't know what to do. The…
Qora bhai
  • 47
  • 6
1
vote
1 answer

Can I add items to a slice in the creation statement depending on a condition?

I have function that creates a slice like this : func buildOptions(cfg *ServerConfig) []SomeType { return []SomeType{ Option1, Option2, Option3, } } I need Option3 to be added to the slice only if a certain…
FunkSoulBrother
  • 2,057
  • 18
  • 27
1
vote
1 answer

How to fix "missing type in composite literal" in test cases

I am trying to write test code for the function ReadField() but I am having difficulty defining the test cases. It gives an error "missing type in composite literal". I believe its just some syntax error. I have tried defining the struct outside…
Jairus Roguel
  • 65
  • 1
  • 8
1
vote
1 answer

Embed an type of other pkg into mine, and init it by literal

I read how to init embed type, and a related Q&A. What my problem is when compile this code, I got : [Error] unknown field 'feature.DefaultSshHelper' in struct literal of type dala02 type FDH feature.DefaultSshHelper …
Frank Wang
  • 788
  • 7
  • 23
1
vote
3 answers

Can I bind one element of class to another while initializing in one line?

I have a class (struct) like this: type Question struct{ Question string answerOne string answerTwo string answerCorrect string } And I initialize it like this: q1:=Question{ Question:"What?", answerOne:"A", …
user8664286
0
votes
2 answers

Golang for loop with multiple brackets

I've stumbled upon this repository, https://github.com/prometheus-community/jiralert/blob/a0f0e80e575e71cbf7db565d3296a3a984282dff/pkg/config/config_test.go#L148 The for loop has multiple brackets: for _, test := range []struct { …
Ratiel
  • 85
  • 7
0
votes
3 answers

Is it possible to have interface composite literals in Go?

I just want to confirm if my understanding is correct about interface{}{} Does interface{}{} mean a composite literal of interface type? So, if I wanted to pass a composite type, lets say []byte as a interface{}, I should assign it to a variable of…
NimaKapoor
  • 119
  • 6
0
votes
1 answer

Go composite literal for type of primitive value

I am a new to Go and have a question. Maybe it is not a idiomatic Go code but just for studying purposes how to make this code work? It seems that I can put as a receiver type of int, but how to call it in main?: xa.go package main import…
0
votes
1 answer

Modifying struct

I have a struct such as type Info struct { Foo string FooBar string Services string Clown string } and lets say I have already populated the first 2 fields input := &Info{ Foo: "true", Services:…
0
votes
3 answers

Golang, what does the following do []

I'm new to golang and have a basic question. I have the following code taken from an example from the web func (d Direction) String() string { return [...]string{"North", "East", "South", "West"}[d] } I'm confused what does the [d] do in the…
pandith padaya
  • 1,643
  • 1
  • 10
  • 20
0
votes
1 answer

Composite literal and fields from an embedded type

I was working on a sample program to answer another question here on SO and found myself somewhat baffled by the fact that the following code will not compile; https://play.golang.org/p/wxBGcgfs1o package main import "fmt" type A struct { …
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0
votes
3 answers

Assigning value in Golang

I create a var of type var RespData []ResponseData ResponseData is a structure as below: type ResponseData struct { DataType string Component string ParameterName string ParameterValue string …
susheel chaudhary
  • 273
  • 3
  • 5
  • 8
1
2