Skip to end of metadata
Go to start of metadata

Syntax and Semantics

Task 1

Write a program that uses control flow statements.


1) If statement 

The expression need not be surrounded by parentheses ( ) but the braces { } are required.

Output:


Similar to C's:

if (expression)
  statement;

 

The expression has to be surrounded by () but {} are not required for a one-line single statement.

2) If with a short statement

An if statement in Go can also contain a short declaration statement preceding the conditional expression.

Output:


doesn't have such statements.

3) If-else statement

Output:

 

Similar to C's:

if (expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

4) For Loop

Output:

 

Similar to C's:

for( expression1; expression2; expression3)
{
   Single statement
   or
   Block of statements;
}

5) Infinite loop

If you omit the loop condition it loops forever.

 

for {
}
      
In C, an infinite loop is like:
for(;;) {
}

 

The difference is that you have to keep the ';'s in C, whereas in Go you can omit the symbols.

6) for loop as while loop

C's while loop is called for in Go:

Output:


Compare with C:

while ( expression )
{
   Single statement 
   or
   Block of statements;
}

 

C uses 'while' but Go uses 'for' as 'while.'

7) Switch case statement

Output:

 

Similar to C's:

switch( expression )
     {
        case constant-expression1:	statements1;
        [case constant-expression2:	statements2;]    
        [case constant-expression3:	statements3;]
        [default : statements4;]
     }

8) Break statement

You can use break statement to break out of a loop before its normal termination.

Output:


C's break statements are similar.

9) Continue statement

The continue statement is used to stop running the loop body midway and continue to the next iteration of the loop.

Output:


C's continue statements are similar.

10) Defer statement

A defer statement defers the execution of a function until the surrounding function returns.

The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

  

Output:
C doesn't have such statements.

Task 2
Write a program to show how functions are treated as a data type in Gp.

Firstly, define a function square that takes in a function as an argument.

 

 

You can assign a function to a variable squareOfNum.

But you don't have to name the function. It can be anonymous. 


You can pass a function named squareOfNum as an argument to another function square.

Output: 

 

If a function is passed as an argument to another function, then such types of functions are known as a Higher-Order function.

As shown in the above example, here square() function takes a function as an argument and returns result float64 as an argument. In the main function, we create an anonymous function whose signature matches the parameter of square function, so we simply call the square() function and pass the result as an argument in it.


Task 3

Implement a general quick sort algorithm that can be used for int and string types and can be used to obtain an ascending or descending order. 

 

I made a partition and a recursive quicksort function to implement the quick sort algorithm.

The partition function takes the last element in the array as the pivot, places the pivot element at correct position in sorted array, and places elements smaller than pivot to the left of pivot and elements greater than pivot to the right of pivot. And it returns the partitioning index used as new pivots in recursive quicksort.
The quicksort sorts the left and right sub-arrays about the pivot.

Output:

 

Extensions

 

Extension 2:

Demonstrate polymorphism in Go.


Firstly, I created an interface Person that contains methods.

Next, I created a struct Student that implements the Person interface. The struct contains fields name and age.

It must provide definitions for the getName and getAge methods.

In between the keyword func and the name of the function getName I've added a “receiver”. The receiver is like a parameter. It has a name s and a type *Student. By creating the function in this way, it allows me to call the function using the . operator.

The function newStudent returns a type Student.

Then, I created another struct Faculty that implements the Person interface and had it implement the same methods.

In my main function, I created two objects of type Student and Faculty and  called the getName and getAge methods on them.


Output:


Extension 5:

Explore the precedence and semantics of memory operators & and * in Go.


In Go, you cannot increment a pointer using ptr++.

Because increment and decrement operations can’t be used as expressions, only as statements. Also, only the postfix notation is allowed.

By removing the pre- and postfix increment operators from the expression hierarchy, expression syntax is simplified and the issues around the order of evaluation are eliminated.

Unary operators have the highest precedence. As the  ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.

In the first example, the pointer points to the address of n at first. Then it gets dereferenced--the value of the pointer is 20. After the increment, the value becomes 21. 

In the second example, the pointer first points to index 0 of the array. The only way to increment the location of pointer is to specify the new index.

After moving the pointer to index 1 of the array, the dereferenced pointer gets evaluated to 2, which is the second element in the array.


Resources

https://tour.golang.org/flowcontrol/12

http://www.golangbootcamp.com/book/control_flow

https://www.callicoder.com/golang-control-flow/

https://www.tutorialspoint.com/ansi_c/c_control_statements.htm

https://golang.org/pkg/sort/#IntSlice

https://www.geeksforgeeks.org/higher-order-function-in-golang/

 

Labels
  • No labels