Signup/Sign In
PUBLISHED ON: JUNE 1, 2021

Go Language Keywords

Keywords are reserved words in Go language and are used to write code. These are special and cannot be used as a user-defined variable or identifier. There are many keywords in Go language. These keywords are used to provide different functionalities that are known to the compiler only.

In Go language, there are 25 keywords present and we have a list of all these keywords in this tutorial.

Go Language Keywords

Let's consider an example where we have used keywords like package, import, func, and var to declare a package, import package, create function, and declare variable respectively.

package main

import (
	"fmt"
)

func main() {
	var name string = "Studytonight"
	fmt.Println("Name is:", name)
}


Name is: Studytonight

Using Go keywords as Identifiers

In Go language, we cannot declare variables with the same name as that of any keyword in Go. If we do so, it will result in an error.

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var func string = "tuts"
	fmt.Println(func)
}

In the above code, we declared a variable named func which is already a keyword in Go. So the above code will throw an error and our program will terminate.


prog.go:8:6: expected 'IDENT', found 'func' (and 2 more errors)

List of Keywords in Go Language

The following table contains keywords present in Go language:

Keyword

Description

break

It is used to break the execution of the loop or in simpler terms, we can say that is used to terminate the loop.

case

The case keyword is used inside the switch statements, and it is used to match a certain condition that we passed in the switch statement.

chan

The chan keyword is used to create a channel in Go. A channel is a way with which goroutines communicate with each other.

continue

The continue keyword is used to pass the control back to the starting of the loop once encountered.

const

The const keyword is used to create a constant in Go.

default

The default keyword is used inside the switch statements and is used to denote the case when none of the above-mentioned cases in the switch statements satisfies the given conditions.

defer

The defer keyword is used when we want certain things to work after the completion of the given function.

else

The else keyword or sometimes also known as the else clause is used in conditional branching, and mainly inside the If/Else statements in Go. It is used when we want to run a certain code that doesn't satisfy the if condition.

fallthrough

The fallthrough keyword in Go is used in the switch statements. In Go, the switch statements are a bit different from traditional ones. As there's no automatic fall through to the next case, so we make use of the fallthrough keyword to provide the same.

for

The for keyword is used in looping. We can create a traditional for loop with the help of the for a keyword, or we can also run a while looking alike loop with the help of the for a keyword.

func

The func keyword is used to create a function in Go. It is generally followed by the name of the function we want to declare

go

The go keyword is used when we want to run asynchronous code in our program. It creates a goroutine and the code becomes asynchronous in nature

goto

The goto keyword in Go is used when we want an unconditional jump to a labeled statement in the same function

if

The if keyword is used when we want to create a conditional check.

import

The import keyword is used when want to import certain third-party or Go standard library packages into our code. The import statement is generally followed by the name of the package that we want to make use of in our program.

interface

The interface keyword is used in Go when we want to create a named collection of method signatures.

map

The map keyword is used to create a map data structure in Go. A map is a collection of key-value pairs in Go.

package

The package keyword in Go is used to declare the name of the package for our program. The name of the package is generally main if it is an executable or else it is the name of the folder it is in.

range

The range keyword or sometimes also known as range clause is used mainly when we are iterating over a ceratin type in Go. It is used with the for keyword when we want to iterate over an iterable object.

return

The return keyword or sometimes also known as a return statement is used mainly in two cases. The first case in which we want to return from the given loop, and the case is generally when we want to return some values when a particular loop exits or terminates.

select

The select keyword is used when we want to wait on multiple Go channel operations.

struct

The struct keyword is used when we want to create a struct in Go

switch

The switch keyword is used to create a switch statement in Go. A switch statement is a conditional statement

type

The type keyword is used when we want to create a new type. The process of creating a new type is called type definition

var

The var keyword is used when we want to create variables in Go.

Note: It should be noted that some of these keywords mentioned above are a bit more advanced. In the upcoming articles, We'll cover all of them, so don't worry.

Conclusion

In the above tutorial, we learned what Keywords are, along with that we also learned about various different keywords that Go provides us, then we used a keyword in Go in a sample program, and lastly, we tried using Go keywords as Identifiers.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.