Pointers in Go programming language or Golang is a variable that is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.).What is the need for the pointers? To understand this need, first, we have to understand the concept of variables. Variables are the names given to a memory location where the actual data is stored. To access the stored data we need the address of that particular memory location. To remember all the memory addresses(Hexadecimal Format) manually is an overhead that's why we use variables to store data and variables can be accessed just by using their name.
Golang also allows saving a hexadecimal number into a variable using the literal expression i.e. number starting from 0x is a hexadecimal number.Example: In the below program, we are storing the hexadecimal number into a variable. But you can see that the type of values is int and saved as the decimal or you can say the decimal value of type int is storing. But the main point to explain this example is that we are storing a hexadecimal value(consider it a memory address) but it is not a pointer as it is not pointing to any other memory location of another variable. It is just a user-defined variable. So this arises the need for pointers.
Go // Golang program to demonstrate the variables // storing the hexadecimal values package main import "fmt" func main() { // storing the hexadecimal // values in variables x := 0xFF y := 0x9C // Displaying the values fmt.Printf("Type of variable x is %T\n", x) fmt.Printf("Value of x in hexadecimal is %X\n", x) fmt.Printf("Value of x in decimal is %v\n", x) fmt.Printf("Type of variable y is %T\n", y) fmt.Printf("Value of y in hexadecimal is %X\n", y) fmt.Printf("Value of y in decimal is %v\n", y) }
Output:
Type of variable x is int Value of x in hexadecimal is FF Value of x in decimal is 255 Type of variable y is int Value of y in hexadecimal is 9C Value of y in decimal is 156
A pointer is a special kind of variable that is not only used to store the memory addresses of other variables but also points where the memory is located and provides ways to find out the value stored at that memory location. It is generally termed as a Special kind of Variable because it is almost declared as a variable but with *(dereferencing operator).

Declaration and Initialization of Pointers
Before we start there are two important operators which we will use in pointers i.e.
- * Operator also termed as the dereferencing operator used to declare pointer variable and access the value stored in the address.
- & operator termed as address operator used to returns the address of a variable or to access the address of a variable to a pointer.
Declaring a pointer:
var pointer_name *Data_Type
Example: Below is a pointer of type string which can store only the memory addresses of string variables.
var s *string
Initialization of Pointer: To do this you need to initialize a pointer with the memory address of another variable using the address operator as shown in the below example:
// normal variable declaration var a = 45 // Initialization of pointer s with // memory address of variable a var s *int = &a
Example:
Go // Golang program to demonstrate the declaration // and initialization of pointers package main import "fmt" func main() { // taking a normal variable var x int = 5748 // declaration of pointer var p *int // initialization of pointer p = &x // displaying the result fmt.Println("Value stored in x = ", x) fmt.Println("Address of x = ", &x) fmt.Println("Value stored in variable p = ", p) }
Output:
Value stored in x = 5748 Address of x = 0x414020 Value stored in variable p = 0x414020
Important Points
1. The default value or zero-value of a pointer is always nil. Or you can say that an uninitialized pointer will always have a nil value.Example:
Go // Golang program to demonstrate // the nil value of the pointer package main import "fmt" func main() { // taking a pointer var s *int // displaying the result fmt.Println("s = ", s) }
Output:
s = <nil>
2. Declaration and initialization of the pointers can be done into a single line.Example:
var s *int = &a
3. If you are specifying the data type along with the pointer declaration then the pointer will be able to handle the memory address of that specified data type variable. For example, if you taking a pointer of string type then the address of the variable that you will give to a pointer will be only of string data type variable, not any other type.
4. To overcome the above mention problem you can use the Type Inference concept of the var keyword. There is no need to specify the data type during the declaration. The type of a pointer variable can also be determined by the compiler like a normal variable. Here we will not use the * operator. It will internally determine by the compiler as we are initializing the variable with the address of another variable.Example:
Go // Golang program to demonstrate // the use of type inference in // Pointer variables package main import "fmt" func main() { // using var keyword // we are not defining // any type with variable var y = 458 // taking a pointer variable using // var keyword without specifying // the type var p = &y fmt.Println("Value stored in y = ", y) fmt.Println("Address of y = ", &y) fmt.Println("Value stored in pointer variable p = ", p) }
Output:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020
5. You can also use the shorthand (:=) syntax to declare and initialize the pointer variables. The compiler will internally determine the variable is a pointer variable if we are passing the address of the variable using &(address) operator to it.Example:
Go // Golang program to demonstrate // the use of shorthand syntax in // Pointer variables package main import "fmt" func main() { // using := operator to declare // and initialize the variable y := 458 // taking a pointer variable using // := by assigning it with the // address of variable y p := &y fmt.Println("Value stored in y = ", y) fmt.Println("Address of y = ", &y) fmt.Println("Value stored in pointer variable p = ", p) }
Output:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020
Dereferencing the Pointer
As we know that * operator is also termed as the dereferencing operator. It is not only used to declare the pointer variable but also used to access the value stored in the variable which the pointer points to which is generally termed as indirecting or dereferencing. * operator is also termed as the value at the address of. Let's take an example to get a better understandability of this concept:Example:
Go // Golang program to illustrate the // concept of dereferencing a pointer package main import "fmt" func main() { // using var keyword // we are not defining // any type with variable var y = 458 // taking a pointer variable using // var keyword without specifying // the type var p = &y fmt.Println("Value stored in y = ", y) fmt.Println("Address of y = ", &y) fmt.Println("Value stored in pointer variable p = ", p) // this is dereferencing a pointer // using * operator before a pointer // variable to access the value stored // at the variable at which it is pointing fmt.Println("Value stored in y(*p) = ", *p) }
Output:
Value stored in y = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020 Value stored in y(*p) = 458
You can also change the value of the pointer or at the memory location instead of assigning a new value to the variable.Example:
Go // Golang program to illustrate the // above mentioned concept package main import "fmt" func main() { // using var keyword // we are not defining // any type with variable var y = 458 // taking a pointer variable using // var keyword without specifying // the type var p = &y fmt.Println("Value stored in y before changing = ", y) fmt.Println("Address of y = ", &y) fmt.Println("Value stored in pointer variable p = ", p) // this is dereferencing a pointer // using * operator before a pointer // variable to access the value stored // at the variable at which it is pointing fmt.Println("Value stored in y(*p) Before Changing = ", *p) // changing the value of y by assigning // the new value to the pointer *p = 500 fmt.Println("Value stored in y(*p) after Changing = ",y) }
Output:
Value stored in y before changing = 458 Address of y = 0x414020 Value stored in pointer variable p = 0x414020 Value stored in y(*p) Before Changing = 458 Value stored in y(*p) after Changing = 500
Similar Reads
Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google
2 min read
Overview
Go Programming Language (Introduction)Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env
11 min read
How to Install Go on Windows?Prerequisite: Introduction to Go Programming Language Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robe
3 min read
How to Install Golang on MacOS?Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but
4 min read
Hello World in GolangHello, World! is the first basic program in any programming language. Letâs write the first program in the Go Language using the following steps:First of all open Go compiler. In Go language, the program is saved with .go extension and it is a UTF-8 text file.Now, first add the package main in your
3 min read
Fundamentals
Identifiers in Go LanguageIn programming languages, identifiers are used for identification purposes. In other words, identifiers are the user-defined names of the program components. In the Go language, an identifier can be a variable name, function name, constant, statement label, package name, or type. Example: package ma
3 min read
Go KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as an identifier. Doing this will result in a compile-time error. Example: C // Go program to illustrate the // use of key
2 min read
Data Types in GoData types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer
7 min read
Go VariablesA typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another
9 min read
Constants- Go LanguageAs the name CONSTANTS suggests, it means fixed. In programming languages also it is same i.e., once the value of constant is defined, it cannot be modified further. There can be any basic data type of constants like an integer constant, a floating constant, a character constant, or a string literal.
6 min read
Go OperatorsOperators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functiona
9 min read
Control Statements
Functions & Methods
Functions in Go LanguageIn Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time. Functions may or may not return a value to the caller.Example:Gopackage main import "fmt" // multiply() multiplies two integers and returns
3 min read
Variadic Functions in GoVariadic functions in Go allow you to pass a variable number of arguments to a function. This feature is useful when you donât know beforehand how many arguments you will pass. A variadic function accepts multiple arguments of the same type and can be called with any number of arguments, including n
3 min read
Anonymous function in Go LanguageAn anonymous function is a function that doesnât have a name. It is useful when you want to create an inline function. In Go, an anonymous function can also form a closure. An anonymous function is also known as a function literal.ExampleGopackage main import "fmt" func main() { // Anonymous functio
2 min read
main and init function in GolangThe Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec
2 min read
What is Blank Identifier(underscore) in Golang?_(underscore) in Golang is known as the Blank Identifier. Identifiers are the user-defined name of the program components used for the identification purpose. Golang has a special feature to define and use the unused variable using Blank Identifier. Unused variables are those variables that are defi
3 min read
Defer Keyword in GolangIn Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don't execute until the nearby functions returns. You can create a deferred m
3 min read
Methods in GolangGo methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu
3 min read
Structure
Arrays
Slices
Slices in GolangSlices in Go are a flexible and efficient way to represent arrays, and they are often used in place of arrays because of their dynamic size and added features. A slice is a reference to a portion of an array. It's a data structure that describes a portion of an array by specifying the starting index
14 min read
Slice Composite Literal in GoThere are two terms i.e. Slice and Composite Literal. Slice is a composite data type similar to an array which is used to hold the elements of the same data type. The main difference between array and slice is that slice can vary in size dynamically but not an array. Composite literals are used to c
3 min read
How to sort a slice of ints in Golang?In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42
2 min read
How to trim a slice of bytes in Golang?In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar
3 min read
How to split a slice of bytes in Golang?In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
3 min read
Strings
Strings in GolangIn the Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where every character is represented by one or more bytes using UTF-8 Encoding. In other words, strings are the immutable chain of arbitrary bytes(including bytes
7 min read
How to Trim a String in Golang?In Go, strings are UTF-8 encoded sequences of variable-width characters, unlike some other languages like Java, python and C++. Go provides several functions within the strings package to trim characters from strings.In this article we will learn how to Trim a String in Golang.Examples := "@@Hello,
2 min read
How to Split a String in Golang?In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
3 min read
Different ways to compare Strings in GolangIn Go, strings are immutable sequences of bytes encoded in UTF-8. You can compare them using comparison operators or the strings.Compare function. In this article,we will learn different ways to compare Strings in Golang.Examplepackage main import ( "fmt" "strings" ) func main() { s1 := "Hello" s2 :
2 min read
Pointers