The Wayback Machine - https://web.archive.org/web/20240906002303/https://www.geeksforgeeks.org/identifiers-in-go-language/
Open In App

Identifiers in Go Language

Last Updated : 20 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In 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 main
import "fmt"

func main() {

 var name = "GeeksforGeeks"
  
}

There is a total of three identifiers available in the above example:

  • main: Name of the package
  • main: Name of the function
  • name: Name of the variable

Rules for Defining Identifiers: There are certain valid rules for defining a valid Go identifier. These rules should be followed, otherwise, we will get a compile-time error.

  • The name of the identifier must begin with a letter or an underscore(_). And the names may contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
  • The name of the identifier should not start with a digit.
  • The name of the identifier is case-sensitive.
  • Keywords are not allowed to be used as an identifier name.
  • There is no limit on the length of the name of the identifier, but it is advisable to use an optimum length of 4 – 15 letters only.

Example:

// Valid identifiers:
_geeks23
geeks
gek23sd
Geeks
geeKs
geeks_geeks

// Invalid identifiers:
212geeks
if
default

Note:

  • In the Go language, there are some predeclared identifiers available for constants, types, and functions. These names are not reserved, you are allowed to use them in the declaration. Following is the list of predeclared identifiers:
For Constants:
true, false, iota, nil

For Types:
int, int8, int16, int32, int64, uint,
uint8, uint16, uint32, uint64, uintptr,
float32, float64, complex128, complex64,
bool, byte, rune, string, error

For Functions:
make, len, cap, new, append, copy, close, 
delete, complex, real, imag, panic, recover
  • The identifier represented by the underscore character(_) is known as a blank identifier. It is used as an anonymous placeholder instead of a regular identifier, and it has a special meaning in declarations, as an operand, and in assignments.
  • The identifier which is allowed to access it from another package is known as the exported identifier. The exported identifiers are those identifiers which obey the following conditions:
    • The first character of the exported identifier’s name should be in the Unicode upper case letter.
    • The identifier should be declared in the package block or be a variable, function, type, or method name within that package.

In the below example, file1.go contains an exported variable called ExportedVariable, which is accessible within the same file. It also imports the file2 package and accesses the exported variable AnotherExportedVariable from file2.go. By running go run file1.go, it will print the value of ExportedVariable (“Hello, World!”) from file1.go and the value of AnotherExportedVariable (“Greetings from file2!”) from file2.go. This demonstrates the concept of exported identifiers being accessible from another package in Go.

Example of file2:

Go




//file2.go
 
package file2
 
// Exported variable
var AnotherExportedVariable = "Greetings from file2!"


Example of file1:

Go




// file1.go
 
package main
 
import (
    "fmt"
    "github.com/yourusername/project/file2"
)
 
// Exported variable
var ExportedVariable = "Hello, World!"
 
func main() {
    // Accessing exported identifier in the same file
    fmt.Println(ExportedVariable)
 
    // Accessing exported identifier from another package
    fmt.Println(file2.AnotherExportedVariable)
}


Output:

Hello, World!
Greetings from file2!
  • The uniqueness of the identifiers means the identifier is unique from the other set of the identifiers available in your program, or in the package and they are not exported.


Previous Article
Next Article

Similar Reads

Select Statement in Go Language
In Go language, the select statement is just like switch statement, but in the select statement, case statement refers to communication, i.e. sent or receive operation on the channel. Syntax: select{ case SendOrReceive1: // Statement case SendOrReceive2: // Statement case SendOrReceive3: // Statement ....... default: // Statement Important points:
4 min read
Loops in Go Language
Go language contains only a single loop that is for-loop. A for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. In Go language, this for loop can be used in the different forms and the forms are: 1. As simple for loop It is similar that we use in other programming languages like C,
5 min read
Loop Control Statements in Go Language
Loop control statements in the Go language are used to change the execution of the program. When the execution of the given loop left its scope, then the objects that are created within the scope are also demolished. The Go language supports 3 types of loop control statements: Break Goto Continue Break Statement The break statement is used to termi
3 min read
Functions in Go Language
Functions are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, provides better readability of the code. So basically, a function is a collection of statements that perform some specific task an
4 min read
Function Returning Multiple Values in Go Language
In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list. Syntax: func function_name(parameter_list)(return_type_list)
3 min read
Anonymous function in Go Language
Go language provides a special feature known as an anonymous function. An anonymous function is a function which doesn't contain any name. It is useful when you want to create an inline function. In Go language, an anonymous function can form a closure. An anonymous function is also known as function literal. Syntax: func(parameter_list)(return_typ
2 min read
Difference Between Go and Python Programming Language
Golang 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 environment adopting patterns alike to dynamic la
2 min read
How to get int63 type random number in Go language?
Go language provides inbuilt support for generating random numbers of the specified type with the help of a math/rand package. This package implements pseudo-random number generators. These random numbers are generated by a source and this source produces a deterministic sequence of values every time when the program run. And if you want to random
2 min read
Auto Format Go Programming Language Source Code with gofmt
Formatting of source code adds an important feature to the quality and readability of the code. In Golang, we have built-in packages and commands to auto-format code in the source file as per the standards and best practices. We can use the gofmt command in the CLI to autoformat a Golang source file. We can autoformat the go code with gofmt package
4 min read
Queue in Go Language
A queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Now if you are familiar with other programming languages like C++, Java, and Python then there are inbuilt queue libraries that can be used for the implementation of queues, but such is not the case in the cas
4 min read
10 Best Books to Learn Go Programming Language [2024]
Golang or Go programming language was introduced first by Google in late 2007 and was released in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson. It’s a free-to-use, open-source language that helps in making more reliable and effective software. There are approximately 1.1 million developers who opt for Go as their primary language. The reaso
10 min read
Golang
Golang is a procedural and statically typed programming language having the syntax similar to C programming language. Sometimes it is termed as Go Programming Language. It provides a rich standard library, garbage collection, and dynamic-typing capability. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launch
2 min read
Learn Programming Languages- List of 11 Popular Programming Languages
In this rapidly growing world, programming languages are also rapidly expanding, and it is very hard to determine the exact number of programming languages. It is an essential part of software development because it creates a communication bridge between humans and computers. Now, if you are a beginner who wants to learn, search the internet, you w
9 min read
Top 10 Golang Project Ideas with Source Code in 2024
Golang, or Go, a programming language was created by Google. It's widely used for building different kinds of applications like websites and cloud services. The fastest way to master this language is by building projects related to it. This article introduces 10 beginner-friendly to medium-difficulty projects in Golang with reference links to sourc
8 min read
Delete Elements in a Slice in Golang
A Slice in Golang is an array(list of elements of the same type) but it's dynamic i.e. it can adjust its size as per the elements added throughout the program. We can initialize a slice without specifying the initial length or size, this allows us to add elements to it dynamically. So, there might be instances, where you need to remove elements fro
7 min read
How to Sort Golang Map By Keys or Values?
Let's say we have a map and want to find a specific key-value pair but it can be located in any order, so to have the map in Golang in a particular order, we can sort the map by its keys or values. In this article, we will see how to sort a map in go lang by its keys or values.  Sort By KeysTo sort a map by keys, we need to first create a list of k
5 min read
Top 5 Golang Frameworks in 2024
Golang (or Go) is an open-source compiled programming language that is used to build simple, systematic, and secure software. It was designed by Google in the year 2007 and has been readily adopted by developers all over the world due to its features like memory safety, structural typing, garbage collection, and similarity to C-language.  Golang we
7 min read
Inheritance in GoLang
Inheritance means inheriting the properties of the superclass into the base class and is one of the most important concepts in Object-Oriented Programming. Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a concept called composition where the struct is used
3 min read
Time Formatting in Golang
Golang supports time formatting and parsing via pattern-based layouts. To format time, we use the Format() method which formats a time.Time object. Syntax: func (t Time) Format(layout string) string We can either provide custom format or predefined date and timestamp format constants are also available which are shown as follows. .time-formatting-i
2 min read
fmt.Sprintf() Function in Golang With Examples
In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. Moreover, this function is defined under the fmt package. Here, you need to import the "fmt" package in order to use t
2 min read
strings.Join() Function in Golang With Examples
strings.Join() Function in Golang concatenates all the elements present in the slice of string into a single string. This function is available in the string package. Syntax: func Join(s []string, sep string) string Here, s is the string from which we can concatenate elements and sep is the separator which is placed between the elements in the fina
1 min read
strings.Contains Function in Golang with Examples
strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax: func Contains(str, substr string) bool Here, str is the original string and substr is the string that you want to check. Let us discuss th
2 min read
Golang program that uses switch, multiple value cases
Switch statement is a multiway branching which provides an alternative way too lengthy if-else comparisons. It selects a single block to be executed from a listing of multiple blocks on the basis of the value of an expression or state of a single variable. A switch statement using multiple value cases correspond to using more than one value in a si
3 min read
strings.Replace() Function in Golang With Examples
strings.Replace() Function in Golang is used to return a copy of the given string with the first n non-overlapping instances of old replaced by new one. Syntax: func Replace(s, old, new string, n int) string Here, s is the original or given string, old is the string that you want to replace. new is the string which replaces the old, and n is the nu
2 min read
time.Sleep() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, this function is defined under the time package. He
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 launched in 2009. It is also known as the Golang
4 min read
How to Split a String in Golang?
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to split a string into a slice with the help of the following functions. These functions are def
5 min read
How to Trim a String in Golang?
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You can trim a string in different ways using the following list of functions. All these functions are defined under strings pa
6 min read
Different ways to concatenate two strings in Golang
In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. In Go strings, the process of adding two or more strings into a new single string is known as concatenation. The simplest way of concatenating two or more strings in the Go language is by using + operator. It is also known as a concatenation operator. E
4 min read
Goroutines - Concurrency in Golang
Go language provides a special feature known as a Goroutines. A Goroutine is a function or method that executes independently and simultaneously in connection with any other Goroutines in your program. In other words, every concurrently executing activity in the Go language is known as a Goroutines. You can consider a Goroutine like a lightweight t
4 min read
Article Tags :