Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 610 Bytes

013. Structure Type.md

File metadata and controls

32 lines (26 loc) · 610 Bytes

Structure Type

Struct

type Box struct {
	  data string
}

func main() {
    var box1 Box
    var box2 Box

    box1.data = "data1"
    box2.data = "data2"

    fmt.Println(box1)
    fmt.Println(box2)
}

// {data1}
// {data2}

구조체는 여러 요소를 가지고 있는 새로운 타입입니다.
구조체를 통해 새로운 타입을 생성할 수 있습니다.

구조체와 메소드

구조체가 사용할 수 있는 함수, 즉 메소드를 생성하기 위해서는 다음과 같이 선언하면 됩니다.

func(box *Box) toEmpty()  {
	  box.data = ""
}