composite-literals

Golang struct literal syntax with unexported fields

我只是一个虾纸丫 提交于 2020-01-23 07:08:06
问题 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 field 'config' in Thing literal . Is there any way I can continue using the literal syntax even though there's now an unexported field on the struct? 回答1: You can only use composite literals to create values of struct types defined in another package if

Assigning value in Golang

有些话、适合烂在心里 提交于 2019-12-31 07:03:03
问题 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 TableValue *[]Rows } type TabRow struct { ColName string ColValue string ColDataType string } type Rows *[]TabRow I want to fill TableValue of type *[]Rows . Can you please tell me with an example by assigning any values in the TableValue . 回答1: TableValue is a pointer to []Rows (slice of Rows ). Rows is

Assigning value in Golang

这一生的挚爱 提交于 2019-12-31 07:02:47
问题 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 TableValue *[]Rows } type TabRow struct { ColName string ColValue string ColDataType string } type Rows *[]TabRow I want to fill TableValue of type *[]Rows . Can you please tell me with an example by assigning any values in the TableValue . 回答1: TableValue is a pointer to []Rows (slice of Rows ). Rows is

Composite literal and fields from an embedded type

那年仲夏 提交于 2019-12-25 18:55:00
问题 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 { FName string LName string } type B struct { A } func (a *A) Print() { fmt.Println(a.GetName()) } func (a *A) GetName() string { return a.FName } func (b *B) GetName() string { return b.LName } func main() { a := &A{FName:"evan", LName:"mcdonnal"} b := &B

What does initializing a Go struct in parentheses do?

a 夏天 提交于 2019-12-07 12:37:13
问题 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 preferred? Here's some Go code to try this out: Playground: https://play.golang.org/p/_gsaruS_DVi 回答1: It does nothing special, those 2 lines are identical. However, when you want to use that in an if statement for example, the parentheses will be

What does initializing a Go struct in parentheses do?

旧街凉风 提交于 2019-12-05 22:38:48
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 preferred? Here's some Go code to try this out: Playground: https://play.golang.org/p/_gsaruS_DVi It does nothing special, those 2 lines are identical. However, when you want to use that in an if statement for example, the parentheses will be required, else you get a compile time error: if i := Item{3, "a"}; i.Id == 3 { } Results in: expected boolean

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

▼魔方 西西 提交于 2019-12-04 05:27:25
问题 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", answerTwo:"B", answerCorrect: ? //I want this have similar value as `answerOne` } While initilizing I want one of my values have similar value as another one. Is there any way to do that? 回答1: You cannot by using only literals, but you could define a function. func NewQuestion() *Question

Golang embedded struct type

给你一囗甜甜゛ 提交于 2019-11-27 03:08:22
问题 I have these types: type Value interface{} type NamedValue struct { Name string Value Value } type ErrorValue struct { NamedValue Error error } I can use use v := NamedValue{Name: "fine", Value: 33} , but I am not able to use e := ErrorValue{Name: "alpha", Value: 123, Error: err} Seems that embedding syntax was ok, but using it doesn't work? 回答1: Embedded types are (unnamed) fields, referred to by the unqualified type name. Spec: Struct types: A field declared with a type but no explicit

Struct in for loop initializer

倖福魔咒の 提交于 2019-11-26 21:51:23
问题 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 []byte err error } go func() { for r := Request{}; r.err == nil; r.id++ { r.line, r.err = input.ReadSlice(0x0a) channel <- r } }() 回答1: Simplifying you code: for r := Request{}; r.err == nil; r.id++ { r.line, r.err = input.ReadSlice(0x0a) channel <- r } Gives