"Go 포인터"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
4번째 줄: 4번째 줄:
<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main
import "fmt"
import "fmt"
func main() {
func main() {
i, j := 42, 2701
i, j := 42, 2701
18번째 줄: 16번째 줄:
*p = *p / 37  // divide j through the pointer
*p = *p / 37  // divide j through the pointer
fmt.Println(j) // see the new value of j
fmt.Println(j) // see the new value of j
}
</syntaxhighlight>
<syntaxhighlight lang='go' run>
package main
import "fmt"
func zeroval(ival int) {
    ival = 0
}
func zeroptr(iptr *int) {
    *iptr = 0
}
func main() {
    i := 1
    fmt.Println("initial:", i)
    zeroval(i)
    fmt.Println("zeroval:", i)
    zeroptr(&i)
    fmt.Println("zeroptr:", i)
    fmt.Println("pointer:", &i)
}
}
</syntaxhighlight>
</syntaxhighlight>

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

1 개요

Go Pointers
package main
import "fmt"
func main() {
	i, j := 42, 2701

	p := &i         // point to i
	fmt.Println(*p) // read i through the pointer
	*p = 21         // set i through the pointer
	fmt.Println(i)  // see the new value of i

	p = &j         // point to j
	*p = *p / 37   // divide j through the pointer
	fmt.Println(j) // see the new value of j
}
package main
import "fmt"
func zeroval(ival int) {
    ival = 0
}
func zeroptr(iptr *int) {
    *iptr = 0
}
func main() {
    i := 1
    fmt.Println("initial:", i)
    zeroval(i)
    fmt.Println("zeroval:", i)
    zeroptr(&i)
    fmt.Println("zeroptr:", i)
    fmt.Println("pointer:", &i)
}

2 같이 보기

3 참고

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