You are on page 1of 14

Programming Fundamentals

Reference Document for Go Syntax


CONTENTS

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

Reference Document for Go Syntax


1. BASIC STRUCTURE
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:

var variableName datatype= value

Example:

var fooBar int=2

3. PRINT STATEMENT

Syntax:

fmt.Println(variable/value) (value/variable)

Example:

fmt.Println("Foo Bar")

4. SELECTION
4.1. IF

CONFIDENTIAL Version No: 1.0 Page 1 of 12


Infosys Limited Reference document for Go syntax

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")
}

CONFIDENTIAL Version No: 1.0 Page 2 of 12


Infosys Limited Reference document for Go syntax

4.3. ELIF LADDER

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")
}

CONFIDENTIAL Version No: 1.0 Page 3 of 12


Infosys Limited Reference document for Go syntax

4.5. SWITCH CASE

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

5.1. FOR LOOP

Syntax:

for init;condition;increment {
//Block of statements
}

Example:

CONFIDENTIAL Version No: 1.0 Page 4 of 12


Infosys Limited Reference document for Go syntax

for foo := 0; foo < 10; foo++{


fmt.Println(a)
}

5.2. FOR USING RANGE

Syntax:

for index,value:=range variableName{


fmt.Println(index,value)
}

Example:

var foo =[]int{1,2,3,4}


for i,v:=range foo{
fmt.Println(i,v)
}

6. BREAK

Syntax:

break

Example:

for i := 1; i<=10; i++ {


if i > 5 { break }
fmt.Println("foo ",i)
}
fmt.Println("Out of for loop...")

CONFIDENTIAL Version No: 1.0 Page 5 of 12


Infosys Limited Reference document for Go syntax

7. CONTINUE

Syntax:

continue

Example:

for i := 1; i<=10; i++ {


if i != 5 { continue }
fmt.Println("foo ",i)
}
fmt.Println("Outof for loop...")

8. ARRAY

Syntax – Declaration of Array

var variableName [size] datatype

Example:

var fooBar [10] int

Syntax – Initialization of Array

var variableName = [size] datatype {values}

Example:

var fooBar = [5] int {1,2,3,4,5}

8.1. APPEND

Syntax:

append(variableName,value)

Example:

CONFIDENTIAL Version No: 1.0 Page 6 of 12


Infosys Limited Reference document for Go syntax

var fooBar = [5] int {1,2,3,4,5}


append(fooBar, 5)

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]

CONFIDENTIAL Version No: 1.0 Page 7 of 12


Infosys Limited Reference document for Go syntax

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)

CONFIDENTIAL Version No: 1.0 Page 8 of 12


Infosys Limited Reference document for Go syntax

11. FUNCTION
Syntax:

func functionName([arg1 datatype,…,argn datatype]) datatype{


//Function body
[return]
}

func main(){
variableName=functionName(values) // Function call
}
Note: return data type must be specified in case return statement is used

Example:

func sum(foo int ,foo_bar int ) int{


return foo+foo_bar}

func main(){
sumOfNumbers:= sum(5,5)}

11.1. POSITIONAL ARGUMENTS

Syntax:

func functionName([arg1 datatype,arg2 datatype]) datatype{


//Function body
[return]
}
func main(){
variableName=functionName(value1,value2) // Function call
}

Example:

func sum(foo int ,foo_bar int ) int{


return foo+foo_bar
}
func main(){
sumOfNumbers:= sum(5,5)
fmt.Println(sumOfNumbers)
}

CONFIDENTIAL Version No: 1.0 Page 9 of 12


Infosys Limited Reference document for Go syntax

11.2. VARIABLE NUMBER OF ARGUMENTS

Syntax:

func function_name(arg...datatype) datatype{


//Function body
[return]
}
func main(){
variableName=functionName(value1,value2)
}

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)
}

CONFIDENTIAL Version No: 1.0 Page 10 of 12


Infosys Limited Reference document for Go syntax

12. VARIABLE SCOPE

12.1. GLOBAL VARIABLE

Syntax:

var variableName datatype=value //global access, can be accessible anywhere.


func functionName([arg1 datatype,…,argn datatype]) datatype{
//Function body
[return]
}
func main(){
variableName=functionName(values) //Function call
}

Example:

var foo int=100


func function1(){
foo =foo+1
fmt.Println(foo)
}
func main(){
function1()}

12.2. LOCAL VARIABLE

Syntax:

func functionName([arg1 datatype,…,argn datatype]) {


var variableName datatype=value //Local access, can accessible only inside
this function.
}

Example:

func function1() {
var foo int=100
foo =foo+1
fmt.Println(foo)
}
func main(){

CONFIDENTIAL Version No: 1.0 Page 11 of 12


Infosys Limited Reference document for Go syntax

function1()
}

CONFIDENTIAL Version No: 1.0 Page 12 of 12

You might also like