Getting started with Go programming – If condition

In the last post we saw the following topics

Go Getting started

Go Strings

Go Datatypes

Go For loop

Now we will see how to work if condition operator.  The syntax of if condition is as follows.

if x > 0 {
 return y

The difference with C here is that the braces are mandatory and the round bracket or parantheses is optional. Remember that {  } is mandatory and (  ) is optional.

[ code title="If condition - Example 1" lang="C" ]
package main
import "fmt"

func main() {
var check_Variable int = 100;

if  check_Variable < 50   {
 fmt.Printf("check_Variable is less than 50\n" );
 } else {
 fmt.Printf("check_Variable is not less than 50\n" );
 fmt.Printf("value of check_Variable is : %d\n", check_Variable);


Here the if condition checks if the variable check_Variable  is less than 50 or not and print the information based on that.

[ code title="If condition - Example 2" lang="C" ]
package main
import "fmt"
import "strings"

func main() {
 fmt.Print("Enter text: ")
 var input string
 if strings.Contains(input, "This") {
      fmt.Println("The string exist")
 }else {
      fmt.Println("This string does not exist")

Here the if condition is used to compare if the input string contains the word “This”. The output is shown below.

code@code:~/Go/GoLangSeries/If$ go run second_if_eg.go
Enter text: Thisisis
The string exist
code@code:~/Go/GoLangSeries/If$ go run second_if_eg.go
Enter text: Notthatword
This string does not exist

You can download the codes from .


Leave a Reply

Your email address will not be published. Required fields are marked *