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

(새 문서: ==개요== ;Go Structs ;Go 구조체 <source lang='go' run> package main import "fmt" type Vertex struct { X int Y int } func main() { fmt.Println(Vertex{1, 2}) } </source> ==...)
 
 
(사용자 2명의 중간 판 9개는 보이지 않습니다)
3번째 줄: 3번째 줄:
;Go 구조체
;Go 구조체


<source lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main
import "fmt"
import "fmt"
type Vertex struct {
type Vertex struct {
X int
X int
Y int
Y int
}
func main() {
fmt.Println(Vertex{1, 2})
}
</syntaxhighlight>
<syntaxhighlight lang='go' run>
// 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() {
func main() {
fmt.Println(Vertex{1, 2})
    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)
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[Go 맵]]
* [[Go 구조체간 형 변환]]
* [[Go 구조체 전달 vs 포인터 전달]]
* [[구조체]]
* [[구조체]]


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


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

2023년 4월 6일 (목) 22:08 기준 최신판

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 }}