Category Archives: Go Language Tutorial

Go Language Tutorial

Go Comments

Go Comments

Go Comments are a set of statements that are not executed by the Go compiler and interpreter. The use of comments makes it easy for humans to understand the source code.Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.

Go Single-line Comments:-

A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character.

Output:-

Go Multi-line Comments:-

If you want to comment multiple lines then you can do it using /* and */, everything in between from /* to */ is ignored by the compiler

Output:-

Go Continue Statement

Go Continue Statement

The continue statement gives you way to skip over the current iteration of any loop. When a continue statement is encountered in the loop, the Go interpreter ignores rest of statements in the loop body for current iteration and returns the program execution to the very first statement in the loop body. It does not terminates the loop rather continues with the next iteration.

Syntax:-

Example:-

Output:-

go_continue_statement

As you can see when ctr == 5, continue statement is executed which causes the current iteration to end and the control moves on to the next iteration.

Go Labeled continue

The standard unlabeled continue statement is used to skip the current iteration of nearest enclosing loop. In GoLang, there is another form of continue (labeled continue ) statement is used to skip iteration of specified loop (can be outer loop). In Golang, Label is an identifier which is followed by colon (:) sign, for example abc:, test:.

Example:-

Output:-

go_labeled_continue_statement

Here, when i == 2 expression is evaluated to true, continue outerfor is executed which skips the current iteration of the enclosed loop and return control to the loop marked with label outerfor:

Go Break Statement

Go Break Statement

In Go, break statement gives you way to break or terminate the execution of innermost “for”, “switch”, or “select” statement that containing it, and transfers the execution to the next statement following it.

Example:-

In this above program, the variable count is initialized as 0. Then a for loop is executed as long as the variable count is less than 10.

Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1).

Next, we have an if statement that checks the variable count is equal to 5, if it return TRUE causes loop to break or terminate.

Within the loop there is a printf() statement that will execute with each iteration of the for loop until the loop breaks.

Then, there is a final println() statement outside of the for loop.

When we run this code, our output will be as follows –

Output:-

go_break_statement

Go Labeled break

The standard unlabeled break statement is used to terminates the nearest enclosing statement. In GoLang, there is another form of break (labeled break) statement is used to terminate specified “for”, “switch”, or “select” statement. In GoLang, Label is an identifier which is followed by a colon (:) sign, for example abc:, test:.

Example:-

Here, when count == 5 expression is evaluated to true, break outer is executed which terminates the loop marked with label outer:.

Output:-

go_labeled_break_statement_1

Go Goto Statement

Go Goto Statement

In GoLang, goto statement is used to alter the normal execution of a program and transfer control to a labeled statement in the same program. The label is an identifier, it can be any plain text and can be set anywhere in the Go program above or below to goto statement. When a goto statement is encountered, compiler transfers the control to a label: and begin execution from there.

Syntax:-

Example:-

Output:-

go_goto_statement

Go For Range

Go For Range

In GoLang, for loop with a “range” clause iterates through every element in an array, slice, string, map, or values received on a channel. A range keyword is used to iterate over slices, arrays, strings, maps or channels.

Synatx:-

here, individual keys and values are held in the corresponding variable and key or value can be omitted if one or the other is not needed using ‘_’

Example:-

Output:-

go_for_each_1

 

Go For Range with Strings

For a string, the for loop iterates over each characters’s Unicode code points. The first value is the starting byte index of the rune and the second the rune itself.

Example:-

Output:-

go_for_each_with_strings

Go For Range with Map

In GoLang, range on map iterates over key/value pairs. It can also iterate over just the keys of a map.

Example:-

Output:-

go_for_each_with_map

Go For Range with Channel

For channel, it iterates over values sent on the channel until closed.

Example:-

Output:-

go_for_each_with_channel

Go For Loop

Go for loop statement

The for loop is used when we want to execute block of code known times. In Go, basic for loop is similar as it is in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required.

In Go, for loop can be used in following forms –

Syntax :-

Property Description
initialization Sets a counter variable
condition It test the each loop iteration for a condition. If it returns TRUE, the loop continues. If it returns FALSE, the loop ends.
increment Increment counter variable

Example:-

Output:-

go_for_loop

Go For loop as a while loop

In GoLang, if we remove the “initialization” and “increment” statement, then the Go for loop behaves like a while loop as in other programming languages.

Example:-

Output:-

go_for_loop_as_while_loop

Go Infinite loop

In GoLang, if we further remove the “condition”, then the Go for loop turns into an infinite loop.

Syntax:-

Example:-

Go Switch Statement

Go Switch Statement

The switch statement is simplified form of the nested if … else if statement , it avoids long chain of if..else if..else statements. A switch statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Syntax:-

Example:-

Output:-

go_switch_statement_1

In Go, you can have multiple values in a single case statement separated with comma (,) as following –

Example :-

Output:-

go_switch_statement_2

Go Switch fallthrough

In switch statement a fallthrough statement allows to execute all the following statements after a match found.

Example:-

Output:-

go_switch_fallthrough

Go Switch with a short statement

In Go, a switch statement can also contain a short declaration statement like the If statement before the conditional expression as following –

Example:-

Output:-

go_switch_with_short_statement

Go switch with no expression

Syntax:-

Example:-

Output:-

go_switch_no_expression

Note:- In Go, a break statement can be used to inside your matched case to exit the switch statement.

Go if else Statement

Go if Statement

If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be true or false.

Syntax:-

Here, you can omit the parentheses () but the curly braces {} are mandatory.

Example:-

Output:-

go_if_statement

Multiple test expressions can also be combined using && (AND) and || (OR) operators as following –

Go if…else Statement

When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.

Syntax:-

Example:-

Output:-

go_if_else_statement

Go Ladder if..else..if Statement

The if..else..if Ladder statement is used to select one of among several blocks of code to be executed.

Example:-

Output:-

go_ladder_if_else

Go Nested if Expression

When there is an if statement inside another if statement then it is known as nested if else.

Example:-

Output:-

go_nested_if_statement

Go If with short statement

In Go, the conditional expression can be preceded by a simple statement, which is executes before the conditional expression is evaluated, and if it is a declaration statement then the variable declared in the statement will only be available inside the if block and it’s else or else-if sections.

Example:-

Output:-

go_if_with_short_statement

Note:- Use of parentheses in case of short statement results in a syntax error.