Keywords in Go

Golang is a programming language that has gained significant popularity in recent years due to its simplicity and readability of syntax, supported by the use of keywords.

Keyword - Keywords or reserved words are words in the language that are used for some internal processes or represent some predefined actions. Therefore, these words are not allowed to be used as identifiers. Doing so will result in compilation errors. Here is what you need to know about keywords in Golang .

Keywords in Go

For example:

// Minh họa chương trình Go sử dụng từ khóa
package main 
import "fmt"

// Từ khóa ở đây là package, import, func, var 
func main() { 

// Đây là một biến hợp lệ
var a = "Quantrimang"

fmt.Println(a) 

// Ở đây, mặc định là một
// định danh không hợp lệ và
// trình biên dịch sẽ đưa ra lỗi
// var default = "GFG" 
} 

Result:

Quantrimang

Go programming currently has a total of 25 keywords:

Keywords in Go

  • break − Used to exit a loop or switch statement.
  • case − Used in a switch statement to specify a possible match for the input value.
  • chan − Used to create communication channels between goroutines.
  • const − Used to define a constant value that cannot be changed.
  • continue − Used to skip the current iteration of a loop and move to the next iteration.
  • defer − Used to schedule a function call to be executed after the current function returns.
  • else − Used to specify an alternative block of code to execute if the if condition is false.
  • fallthrough − Used in a switch statement to specify that control will pass to the next case.
  • for − Used to create a loop that repeats a block of code a specified number of times.
  • func − Used to define a function that can be called from other parts of the program.
  • go − Used to start a new goroutine.
  • goto − Used to jump to a specific label within the current function.
  • if − Used to execute a block of code only if a certain condition is true.
  • import − Used to import a package into the program.
  • interface − Used to define a set of methods that a type must implement.
  • map − Used to define a set of key-value pairs.
  • package − Used to define a package containing one or more Go source files.
  • range − Used to iterate over an array, slice, sequence, map, or channel.
  • return − Used to exit a function and return a value to the caller.
  • select − Used to wait for a value to be sent to one of multiple channels.
  • struct − Used to define a set of fields representing a complex data type.
  • switch − Used to execute a block of code based on the value of an expression.
  • type − Used to define a new data type.
  • var − Used to declare a variable.

For example:

// Minh họa chương trình Go sử dụng từ khóa

// Từ khóa package được dùng để bao gồm main package trong chương trình
package main 

// Từ khóa import được dùng để nhập "fmt" vào trong package 
import "fmt"

// func được dùng để tạo hàm
func main() { 

	// Từ khóa var được dùng để tạo biến
	// Pname, Lname, and Cname là các biến hợp lệ
	var Pname = "Quantrimang"
	var Lname = "Go Language"
	var Cname = "Keywords"
	
	fmt.Printf("Portal name: %s", Pname) 
	fmt.Printf("\nLanguage name: %s", Lname) 
	fmt.Printf("\nChapter name: %s", Cname) 

} 

Result:

Portal name: GeeksforGeeks
Language name: Go Language
Chapter name: Keywords

Whether you are a beginner or an experienced developer, mastering Golang keywords is an important step towards becoming a proficient Go programmer.

Sign up and earn $1000 a day ⋙

Leave a Comment

How to change units of measurement on Apple Maps

How to change units of measurement on Apple Maps

Apple Maps application supports you to change the measurement unit between km, mile, and miles, depending on each person's needs and usage habits.

7 ChatGPT and AI Apps for Free Travel Planning

7 ChatGPT and AI Apps for Free Travel Planning

These free travel planning apps use AI and Machine Learning tools like ChatGPT to prepare your itinerary in seconds.

How to use Gemini from Google Chrome address bar

How to use Gemini from Google Chrome address bar

Google is integrating AI features into the Google Chrome browser, most recently using Gemini from the Google Chrome address bar.

4 Ways to Change User Account Type in Windows

4 Ways to Change User Account Type in Windows

In addition to creating multiple users, Windows 10 also lets you limit what others can do on a shared PC, by providing multiple types of accounts, namely standard user accounts and admin accounts.

10 useful shortcuts to increase productivity on Microsoft Edge

10 useful shortcuts to increase productivity on Microsoft Edge

Keyboard shortcuts are a great productivity tool, especially for those whose jobs involve a lot of typing.

Serious bug forces Google to pause AI feature that creates images with text

Serious bug forces Google to pause AI feature that creates images with text

Google has announced that it is suspending the feature of generating images of people from text of its Gemini AI model due to a serious historical error.

How to Use Copilot in PowerPoint

How to Use Copilot in PowerPoint

Copilot will help you add creative ideas to your PowerPoint content slides, or fix the bad content in your presentation slides.

How to delete Google Gemini conversation

How to delete Google Gemini conversation

Instead of deleting your entire Google Gemini history, you can choose to delete the conversations you want. This article will guide you through deleting your Google Gemini conversations.

How to Downgrade from iOS 16 Beta 1 to iOS 15

How to Downgrade from iOS 16 Beta 1 to iOS 15

In this article, WebTech360 will guide you how to downgrade from iOS 16 Beta to iOS 15.

How to fix #SPILL! error in Microsoft Excel

How to fix #SPILL! error in Microsoft Excel

The #SPILL! error in Excel is quite common and it is quite simple to fix. Here is how to fix the #SPILL! error easily in Excel.

The best apps to monitor children using phones

The best apps to monitor children using phones

Here's a list of the best parental control apps, to help you compare the different options available.

How to pin media controls to Chromebook Shelf

How to pin media controls to Chromebook Shelf

To use this feature, your Chromebook must be running Chrome OS 89 or later.

How to Stop Sharing Safari Web History Across Multiple Devices

How to Stop Sharing Safari Web History Across Multiple Devices

Safari browser shares web history with other Apple devices that share the same iCloud account. Many people concerned about privacy can refer to the article to stop sharing.

How to Resize Images Properly in Photoshop

How to Resize Images Properly in Photoshop

Many people used to find resizing images very annoying until they learned about Photoshop's simple channel-based protection method.

How to avoid the most common Valentines Day scams

How to avoid the most common Valentines Day scams

Valentine's Day is prime time for scammers. However, you can ensure your February 14th goes smoothly by avoiding these scams.