English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Functions & Methods

Golang Structs

Golang Slices & Arrays

Golang Strings(String)

Golang Pointers

Golang Interfaces

Golang Concurrency

Golang Exceptions(Error)

Other Golang items

Go Language Function Returns Pointer

In the Golang programming language, a pointer is a variable used to store the memory address of another variable. We can pass a pointer to a function, or return a pointer from a function in Golang. In C/ c++It is not recommended to return the address of a local variable outside a function, as it goes out of scope after the function returns. Therefore, in C/ c++In C, when a function returns a pointer, it is necessary to define the local variable as a static variable.

Example:Let me take a look at a C++Example, in the following program, the code line (int lv = n1 * n1;) will generate a warning because it is the local code of the function. To avoid the warning, set it to a static variable.

// C ++Program returns
//Pointer from the function
#include <iostream> 
using namespace std; 
  
//Accepts a pointer as a return type
int* rpf(int); 
  
int main() 
{ 
  
    int n = 745; 
  
    //Display the value of n
    cout << n << endl; 
  
    //Call the function
    cout << *rpf(n) << endl; 
} 
  
//Define function 
int* rpf(int n1) 
{ 
  
        //Take local variables
        //Internally in the function
    int lv = n1 * n1; 
  
        
    // static int lv = n1 * n1; 
  
    //C++ This will give us a warning
    //Return address
    return &lv; 
}

Warning items

prog.cpp: In function ‘int* rpf(int)’:
prog.cpp:24:9: warning: address of local variable ‘lv’ returned [-Wreturn-local-addr]
int lv = n1 * n1;

Output:

745

The main reason in this case is that the compiler always generates a stack for function calls. Once the function exits, the function stack is also deleted, which causes the local variables of the function to be out of scope. Setting it to static will solve this problem. Since static variables have the property of retaining their values even when they are out of scope.

HoweverThe Go compiler is very smart!. It will not allocate memory on the stack for the local variables of the function. It will allocate this variable on the heap. In the following program, the variablelvMemory will be allocated on the heap because the Go compiler will perform escape analysis to escape variables from the local scope.

//Go function returns a pointer
package main
import "fmt"
func main() {
    //Call the function
    n := rpf()
    //Display the value
    fmt.Println("The value of n: ", *n)
}
//Define a function with an integer
//Pointer as return type
func rpf() *int {
    //Local variables
    //Short operator declarations are used internally in the function
    lv := 100
    // Return the address of lv
    return &lv
}

Output:

The value of n:  100

Note: Golang does not provide an object like C / C ++Any support for such pointer algorithms. If executed, the compiler will trigger an error, considering it an invalid operation.

Related knowledge: Go languagePointerAnd WillPointer Passed to Function