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

5번째 줄: 5번째 줄:
<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main
import "fmt"
import "fmt"
// vertex = 정점(頂點)
type Vertex struct {
type Vertex struct {
X int
X int
Y int
Y int
}
}
func main() {
func main() {
fmt.Println(Vertex{1, 2})
fmt.Println(Vertex{1, 2})
20번째 줄: 16번째 줄:


<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
// https://gobyexample.com/structs
package main
package main
import "fmt"
import "fmt"
type person struct {
type person struct {
     name string
     name string

2021년 9월 25일 (토) 01:26 판

1 개요

Go Structs
Go 구조체
package main
import "fmt"
type Vertex struct {
	X int
	Y int
}
func main() {
	fmt.Println(Vertex{1, 2})
}
// https://gobyexample.com/structs
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 }}