Professional Documents
Culture Documents
PF Go Syntax PDF
PF Go Syntax PDF
CONTENTS .............................................................................................................................................................................. ii
Reference Document for Go Syntax .......................................................................................................................................... 1
1.BASIC STRUCTURE ........................................................................................................................................................ 1
2.VARIABLE ........................................................................................................................................................................ 1
3.PRINT STATEMENT ........................................................................................................................................................ 1
4.SELECTION....................................................................................................................................................................... 1
4.1. IF ................................................................................................................................................................................ 1
4.2. IF ELSE ..................................................................................................................................................................... 2
4.3. ELIF LADDER .......................................................................................................................................................... 3
4.4. NESTED IF................................................................................................................................................................ 3
4.5. SWITCH CASE ......................................................................................................................................................... 4
5.ITERATION ....................................................................................................................................................................... 4
5.1. FOR LOOP ................................................................................................................................................................ 4
5.2. FOR USING RANGE ................................................................................................................................................ 5
6.BREAK ............................................................................................................................................................................... 5
7.CONTINUE ........................................................................................................................................................................ 6
8.ARRAY .............................................................................................................................................................................. 6
8.1. APPEND .................................................................................................................................................................... 6
8.2. SORT ......................................................................................................................................................................... 7
10.LIBRARIES ...................................................................................................................................................................... 7
10.1. STRING ................................................................................................................................................................... 7
10.2. TIME........................................................................................................................................................................ 8
10.3. MATH...................................................................................................................................................................... 8
11.FUNCTION ...................................................................................................................................................................... 9
11.1. POSITIONAL ARGUMENTS ................................................................................................................................ 9
11.2. VARIABLE NUMBER OF ARGUMENTS ......................................................................................................... 10
12.VARIABLE SCOPE ....................................................................................................................................................... 11
12.1. GLOBAL VARIABLE .......................................................................................................................................... 11
12.2. LOCAL VARIABLE ............................................................................................................................................. 11
CONFIDENTIAL
Infosys Limited Reference document for Go syntax
package main
import ("fmt")
func main(){
fmt.Println(variable/value)
}
Example:
package main
import("fmt")
func main(){
fmt.Println("Foo Bar")
}
2. VARIABLE
Syntax:
Example:
3. PRINT STATEMENT
Syntax:
fmt.Println(variable/value) (value/variable)
Example:
fmt.Println("Foo Bar")
4. SELECTION
4.1. IF
Syntax:
if condition {
//block of statements
}
Example:
if foo>3 {
fmt.Println ("foo is greater than 3")
}
4.2. IF ELSE
Syntax:
if condition {
//block of statements
} else {
//block of statements
}
Example:
if foo > 3 {
fmt.Println ("foo is greater than 3")
} else {
fmt.Println ("foo is not greater than 3")
}
Syntax:
if(condition){
//block of statements
}else if(condition){
//block of statements
}else{
//block of statements
}
Example:
if foo == 1{
fmt.Println ("foo equals 1")
}else if foo == 2 {
fmt.Println ("foo equals 2 ")
} else {
fmt.Println ("foo equals 3")
}
4.4. NESTED IF
Syntax:
if condition {
//block of statements
if condition {
} else {
//block of statements
} } else {
//block of statements
}
Example:
if foo > 0 {
if foo > 30 {
fmt.Println ("foo is greater than 30")
} else {
fmt.Println ("foo is not greater than 30")
} } else {
fmt.Println ("foo is not greater than 0")
}
Syntax:
switch(expression){
case value1:
//statements
break
case value2:
//statements
break
case valueN:
//statements
break
default:
//statements
}
Example:
switch(foo){
case 1:
fmt.Println("foo is equal to 1")
break
case 2:
fmt.Println("foo is equal to 2")
break
case 3:
fmt.Println("foo is equal to 3")
break
default:
fmt.Println("foo is not a number")
}
5. ITERATION
Syntax:
for init;condition;increment {
//Block of statements
}
Example:
Syntax:
Example:
6. BREAK
Syntax:
break
Example:
7. CONTINUE
Syntax:
continue
Example:
8. ARRAY
Example:
Example:
8.1. APPEND
Syntax:
append(variableName,value)
Example:
8.2. SORT
Syntax:
import (
"sort"
)
sort.Ints(sampleArray)
Example:
import (
"sort"
)
var fooBar = [5] int {1,2,3,4,5}
sort.Ints(fooBar)
10. LIBRARIES
10.1. STRING
Syntax:
import (
"strings"
)
strings.Replace(variable ,"old_string","new_string",-1)
strings.Index(variable,"string_to_find")
strings.HasPrefix(variable,"string_to_match")
strings. HasSuffix(variable,"string_to_match")
strings.ToUpper(variable)
strings.ToLower(variable)
strings.Split(variable," ")
Example:
import ("strings")
var foo string="I love python"
strings.Replace(foo,"l","a",-1)
strings.Index(foo,"python")
strings.HasPrefix(foo,"I")
strings. HasSuffix(foo,"on")
strings.ToUpper(foo)
strings.ToLower(foo)
strings.Split(foo," ")foo.split(" ")
foo[1:4]
10.2. TIME
Syntax:
import (
"time"
)
time.Now().Local()
time.Now().Location()
time.Now().UTC()
Example:
import (
"time"
)
foo1:=time.Now().Local()
foo2:=time.Now().Location()
foo3:=time.Now().UTC()
10.3. MATH
Syntax:
import (
"math"
)
math.Ceil(decimal_value)
math.Floor(decimal_value)
math.Abs(negative value)
Example:
import (
"math"
)
math.Ceil(9.6)
math.Floor(9.6)
math.Abs(-9.6)
11. FUNCTION
Syntax:
func main(){
variableName=functionName(values) // Function call
}
Note: return data type must be specified in case return statement is used
Example:
func main(){
sumOfNumbers:= sum(5,5)}
Syntax:
Example:
Syntax:
Example:
func sum(foo...int){
fooBar:=0
for index,sum:=range foo{
fmt.Println("index position is ",index)
fooBar=fooBar+sum
}
fmt.Println("sum is ",fooBar)
}
func main(){
sum(1, 2)
sum(1, 2, 3)
}
Syntax:
Example:
Syntax:
Example:
func function1() {
var foo int=100
foo =foo+1
fmt.Println(foo)
}
func main(){
function1()
}