Last active
August 13, 2019 15:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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