Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/b123e4a98bcf232d09216577c29f34a3 to your computer and use it in GitHub Desktop.
Save Integralist/b123e4a98bcf232d09216577c29f34a3 to your computer and use it in GitHub Desktop.
[golang avoid promoted field in literal struct error] #go #golang #struct #promoted #field #error
package main
import (
"fmt"
)
type A struct {
enabled bool
}
func (a *A) isEnabled() bool {
fmt.Println("A isEnabled")
return a.enabled
}
type B struct {
A
}
func (b *B) isEnabled() bool {
fmt.Println("B isEnabled")
return b.A.isEnabled()
}
func main() {
/*
a := &A{enabled: true}
b := &B{enabled: false}
fmt.Printf("a: %#v\n", a.isEnabled())
fmt.Printf("b: %#v\n", b.isEnabled())
// cannot use promoted field A.enabled in struct literal of type B
*/
var a A
a.enabled = true
var b B
b.enabled = false
fmt.Printf("a: %#v\n", a.isEnabled())
fmt.Printf("b: %#v\n", b.isEnabled())
/*
A isEnabled
a: true
B isEnabled
A isEnabled
b: false
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment