I was studying that is-a relationship about embeding.
But example was not working below refer.
refer : https://up-to-date-items.tistory.com/114
package main
import "fmt"
type Player struct {
salary int
}
type Pro struct {
Player
bonus int
}
func (p Player) getSalary() int {
return p.salary
}
func main() {
player1 := Player{salary: 100}
proPlayer := Pro{Player{salary: 120}, bonus: 30}
fmt.Println(player1.getSalary())
fmt.Println(proPlayer.getSalary() + proPlayer.bonus)
}
https://go.dev/play/p/5Pn3_p9lvuF
Solution is like below
proPlayer := Pro{Player{120}, 30}
or
proPlayer := Pro{Player: Player{salary: 120}, bonus: 30}
1) Only values
2) Field:Value
example : https://go.dev/play/p/ZKhuBmXD-_Y
Sprint는 어떤 때 사용되는가? (0) | 2022.03.26 |
---|---|
Quick Sort in Go (0) | 2022.03.17 |
28/2 Struct Pointer study (0) | 2022.03.01 |
27/02 Builder pattern in GO - Construct the ship's Defense Shields (4) | 2022.02.27 |
댓글 영역