Go 또는 Golang에서 포인터 변수를 만드는 방법은 무엇입니까?
                                            
                                                
                                                
                                                
                                                
                                                
                                                 6994 단어  go
                    
Go 또는 Golang에서 포인터 변수를 만들려면
var 키워드 다음에 포인터 변수 이름, * 기호(별표), 마지막으로 메모리 주소를 유지하는 데 필요한 변수 유형을 사용할 수 있습니다. 공백없이.TL;DR
package main
import "fmt"
func main() {
    // a simple variable
    name := "John Doe"
    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string
    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name
    // log the memory address
    fmt.Println(nameAddr) // 0xc000014250 <- this is the address in my system, yours may be different
}
예를 들어, 다음과 같이
name의 string를 갖는 John Doe라는 변수가 있다고 가정해 보겠습니다.package main
func main(){
    // a simple variable
    name := "John Doe"
}
이제
name 변수의 메모리 주소를 저장할 포인터 변수를 만들어 보겠습니다. 포인터 변수nameAddr를 호출해 봅시다.다음과 같이 할 수 있습니다.
package main
func main(){
    // a simple variable
    name := "John Doe"
    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string
}
위의 코드에서 볼 수 있듯이
var 키워드 다음에 포인터 변수 이름 nameAddr , * 기호 및 string 유형을 공백 없이 사용했습니다. 우리는 string 유형을 갖는 name 변수의 메모리 주소를 보유하고 싶기 때문에 string 유형을 사용했습니다.마지막으로
name 변수의 메모리 주소를 할당하기 위해 & 변수 앞에 name 기호(앰퍼샌드)를 사용하여 name 변수의 메모리 주소를 얻을 수 있습니다. 그런 다음 nameAddr 연산자(할당)를 사용하여 해당 값을 = 변수에 할당할 수 있습니다.다음과 같이 할 수 있습니다.
package main
func main(){
    // a simple variable
    name := "John Doe"
    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string
    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name
}
이제
name 포인터 변수를 사용하여 nameAddr 변수의 메모리 주소를 다음과 같이 터미널에 기록해 보겠습니다.package main
import "fmt"
func main() {
    // a simple variable
    name := "John Doe"
    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string
    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name
    // log the memory address
    fmt.Println(nameAddr) // 0xc000014250 <- this is the address in my system, yours may be different
}
Go에서 포인터 변수를 성공적으로 만들었습니다. 예이 🥳!
The Go Playground에 있는 위의 코드를 참조하십시오.
그게 다야 😃.
이 정보가 유용하다고 생각되면 자유롭게 공유하세요 😃.
Reference
이 문제에 관하여(Go 또는 Golang에서 포인터 변수를 만드는 방법은 무엇입니까?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/melvin2016/how-to-create-a-pointer-variable-in-go-or-golang-2mo5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)