A Programmer's Guide To Go With Visual Studio Code
Written by Mike James   
Monday, 08 July 2019
Article Index
A Programmer's Guide To Go With Visual Studio Code
Variables & Structs
Types
Parameters
Go Control

 

You can leave out any of the components of the for statement but you have to put the semicolons in to show which parts you are leaving out. The only exception to this rule is the condition which you can simply write without semi-colons to create the conditional form of the for. 

You can also iterate through a range of values in an array, slice, string, map or channel. This is Go's equivalent of the for each loop in other languages. 

For example

var array= [] int {1,2,3,4}
for i,v:= range array { 
 fmt.print(i,v)
}

The range form of the for loop iterates over the index and the value of sequences i.e. i and v in this case. 

There are only two other control statements in Go. The if is much like it is in other languages but without the brackets around the condition. For example:

if a<0 { 
 fmt.print("Negative")
} else { 
 fmt.print("Positive")
}

You don't have to use an else but you do need the curly brackets.  For example:

if a<0 { 
 fmt.print("Negative")
}

You can create multiple if statements by following else with another if. 

if a<0 { 
 fmt.print("Negative")
} else if a==0 { 
 fmt.print("Zero")
} else {
  fmt.print("Positive")
}

 

You can also include an initial statement which is executed before the if:

if a:=myfunc() a<0 { 
 fmt.print("Negative")
}

Any variables created are only in scope for the if statement. 

There is also a switch statement for multiway selections on a single expression. For example

switch a {
 case 0,1:
  
fmt.print("a is 0 or 1)
 
case 2.3:
  
fmt.print("a is 2 or 3)
 default: 
  fmt.print("a is some other value")
}

You can also write conditions on each of the case statements as in

case a<0:

There is no need to use break to jump out of a select but you do need to use fallthrough if you want cases to run into each other. The case statements are executed in order and the first one that succeeds causes a break - so the default has to be at the end in most cases. 

As well as being able to write a select on values and conditions you can also write a switch on types. For example;

switch a.type{
 
case int: 
   fmt.print("a is an int")
 case float64: 
   fmt.print("a is a float")
 default: 
   fmt.print("some other type")
}

 

Finally there are the break and continue statements that can be used within a for loop to either terminate the loop or terminate the current iteration respectively. The break can also be used to jump out of a switch.

There is a goto, but this isn't worth talking about.

goruns

 

More Information:

http://golang.org

Related articles:

Go Programming Language Turns 3       

Getting started with Google's Go

Why invent a new language? Go creator explains

Ready to Go - Go Reaches Version 1

Go in Google App Engine

Google App Engine Go-es Forward

Go with Google - Yet Another Language!

 

Banner

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

 

 



Last Updated ( Monday, 08 July 2019 )