![]() |
VOOZH | about |
Go, often referred to as Golang, is an open-source programming language developed by Google in 2007 and officially released in 2009. It was designed with the goal of combining performance and security of a low-level language like C with the simplicity and productivity of modern languages like Python.
If you're looking to learn Go programming,Boot.dev's Full Go Course is the perfect starting point. With step-by-step lessons, hands-on challenges, and expert guidance, you'll build a solid foundation in Go. Dive into real-world projects and gain the skills to become a proficient Go developer. Enroll now and master Go at your own pace!.
To run a Go program you need a Go compiler. In Go compiler, first you create a program and save your program with extension .go, for example, first.go.
Output:
Hello, geeksforgeeksExplanation of the syntax of Go program:
Single Line Comment:
Syntax:
// single line commentMulti-line Comment:
Syntax:
/* multiline comment */Now we run this first.go file in the go compiler using the following command, i.e:
go run first.goFor more details about the different terms used in this program, you can visit Hello World in Golang
There are various online IDEs such as The Go Playground, repl.it, etc. which can be used to run Go programs without installing. For installing Go in own PCs or Laptop we need of following two software: Text editor and Compiler.
Note: Extension of source code file of go language must be .go
Identifiers are the user-defined name of the program components. In Go Programming language, an identifier can be a variable name, function name, constant, statement labels, package name, or types.
Example:
// Valid identifiers:
_geeks23
geeks
gek23sd
Geeks
geeKs
geeks_geeks
// Invalid identifiers:
212geeks
if
defaultKeywords 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. There are total 25 keywords present in the Go language as follows:
Output:
Portal name: GeeksforGeeks
Language name: Go Language
Chapter name: KeywordsData 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:
Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are:
Numbers: In Go language, numbers are divided into three sub-categories that are:
Floating-Point Numbers: In Go language, floating-point numbers are divided into two categories as shown in the below table:
Complex Numbers: The complex numbers are divided into two parts are shown in the below table. float32 and float64 are also part of these complex numbers. The in-built function creates a complex number from its imaginary and real part and in-built imaginary and real function extract those parts.
Variable names 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 ā_ā.
Geeks, geeks, _geeks23 // valid variable
123Geeks, 23geeks // invalid variable234geeks // illegal variable geeks and Geeks are two different variablesif : It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition) {
// Statements to execute if
// condition is true
}
Flow Chart:
if-else : if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition) {
// Executes this block if
// condition is true
} else {
// Executes this block if
// condition is false
}Flow Chart:
Nested if : Nested if statements mean an if statement inside an if statement. Yes, Golang allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}Flow Chart:
if-else-if Ladder : Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Some popular Applications developed in Go Language