ADVERTISEMENT
ADVERTISEMENT

Golang Keywords

Golang | Keywords: In this tutorial, we are going to learn about the all Golang keywords.
Submitted by IncludeHelp, on July 23, 2021

Keywords are reserved words whose meanings are pre-defined in the compiler and we cannot change their meaning. Keywords can also not be used for any other purpose like using them as identifiers. If we use keywords as the identifier, this will generate an error.

List of Golang keywords

There are 25 keywords in Golang, they are:

breakcasechanconstcontinue
defaultdeferelsefallthroughfor
funcgogotoifimport
interfacemappackagerangereturn
selectstructswitchtypevar

Consider the below program,

package main

import (
	"fmt"
)

func main() {
	var name = "Alex"
	fmt.Println("Hey! My name is", name)
}

Output:

Hey! My name is Alex

In the above program, the keywords are: package, import, func, and var.

What happens if we use a keyword as an identifier?

If we use a keyword as an identifier in Golang (and, other programming languages also), the program troughs an error.

package main

import (
	"fmt"
)

func main() {
	// Here, we are using keyword "range"
	// as an identifier (i.e., variable name)
	var range = "Alex"
	fmt.Println("Hey! My name is", range)
}

Output:

./prog.go:10:6: syntax error: unexpected range, expecting name
./prog.go:11:33: syntax error: unexpected range, expecting expression

In the program, we are using the keyword range as a variable name to store a string and the result is a syntax error.


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.