상세 컨텐츠

본문 제목

28/2 mixture of field:value and value initializers error in Go

Go/Study record

by Gopythor 2022. 2. 28. 17:46

본문

728x90
반응형

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

728x90
반응형

'Go > Study record' 카테고리의 다른 글

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

관련글 더보기

댓글 영역