"Go 구조체"의 두 판 사이의 차이

64번째 줄: 64번째 줄:


==참고==
==참고==
*https://tour.golang.org/moretypes/2
* https://tour.golang.org/moretypes/2
* https://gobyexample.com/structs


[[분류: Go 구조체]]
[[분류: Go 구조체]]

2021년 4월 9일 (금) 10:40 판

1 개요

Go Structs
Go 구조체
package main

import "fmt"

// vertex = 정점(頂點)
type Vertex struct {
	X int
	Y int
}

func main() {
	fmt.Println(Vertex{1, 2})
}
package main

import "fmt"

type person struct {
    name string
    age  int
}

func newPerson(name string) *person {

    p := person{name: name}
    p.age = 42
    return &p
}

func main() {

    fmt.Println(person{"Bob", 20})

    fmt.Println(person{name: "Alice", age: 30})

    fmt.Println(person{name: "Fred"})

    fmt.Println(&person{name: "Ann", age: 40})

    fmt.Println(newPerson("Jon"))

    s := person{name: "Sean", age: 50}
    fmt.Println(s.name)

    sp := &s
    fmt.Println(sp.age)

    sp.age = 51
    fmt.Println(sp.age)
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}