0% found this document useful (0 votes)
15 views30 pages

Go Record

The document contains a series of exercises aimed at teaching basic programming concepts in Go, including arithmetic, relational, logical operations, constants, loops, branching, switch statements, arrays, and slices. Each exercise includes a program, output, and a conclusion stating that the program was successfully completed. The procedures outline steps for writing and testing the programs, along with explanations of the concepts.

Uploaded by

naveenkumarmg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

Go Record

The document contains a series of exercises aimed at teaching basic programming concepts in Go, including arithmetic, relational, logical operations, constants, loops, branching, switch statements, arrays, and slices. Each exercise includes a program, output, and a conclusion stating that the program was successfully completed. The procedures outline steps for writing and testing the programs, along with explanations of the concepts.

Uploaded by

naveenkumarmg0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

EX.

NO : 01 ​ ​ ​ ​ ​ ​ ​ NAME :

DATE:​​ ​ ​ ​ ​ REG.NO:

Write a program to understand arithmetic operations in Go.

Aim: To write a program on arithmetic operations using go

Procedure

1.​ Open a text editor on your computer.


2.​ Create a new file and save it with the extension ".go".
3.​ Define the necessary data structures, such as variables or constants.
4.​ Test the program by running it with different inputs and verifying that it produces the
expected output.
5.​ If any issues or limitations are identified during testing, optimize the program to address
them.
6.​ Once the program is complete and has been thoroughly tested, document it by creating a
lab record that summarizes the program's purpose, inputs, outputs, and performance.

Program

package main

import "fmt"

func main() {

var a int =21

var b int =10

var c int

c =a + b

fmt.Printf("Line 1-value of c is %d\n",c)

c =a - b

fmt.Printf("Line 2-value of c is %d\n",c)

c =a * b

fmt.Printf("Line 3-value of c is %d\n",c)


1
c =a / b

fmt.Printf("Line 4-value of c is %d\n",c)

c =a % b

fmt.Printf("Line 5-value of c is %d\n",c)

a++

fmt.Printf("Line 6-value of c is %d\n",a)

a--

fmt.Printf("Line 7-value of c is %d\n",a)

OUTPUT:

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1

Line 6 - Value of a is 22

Line 7- Value of a is 21

Result

Thus the above program was successfully completed

2
EX.NO : 02​ ​ ​ ​ ​ ​ ​ NAME :

DATE:​​ ​ ​ ​ ​ REG.NO:

Write a program to understand relational operations in Go.

Aim: To Write a program on relational operations in Go.

Procedure

1.​ Open a text editor on your computer.


2.​ Create a new file and save it with the extension ".go".
3.​ Define variables to be used in relational operations.
4.​ Write code to perform basic relational operations, using the appropriate syntax for
equality, inequality, greater than, less than, greater than or equal to, and less than.
5.​ Understand the use of logical operators (&&, ||,!) to combine relational operations and
create more complex expressions.
6.​ Test the program by running it with different inputs and verifying that it produces the
expected output.
7.​ Once you understand relational operations in go, document what you have learned by
creating a lab record that summarizes the syntax and use of relational operations in go.
Program

package main

import "fmt"

func main() {

var a int =21

var b int =10

if (a == b) {

fmt.Printf("Line 1 - a is equal to b\n")

}else {

fmt.Printf("Line 1- a is not equal to b\n")

3
if(a<b) {

fmt.Printf("Line 2 - a is less thann b\n")

}else {

fmt.Printf("Line 2 - a is not less than b\n")

} if (a>b){

fmt.Printf("Line 3 - a is greater than b\n")

}else {

fmt.Printf(" Line 3 -a is not greater than b\n")

} /* Lets change the value of a and b*/

a=5

b = 20

if (a <= b){

fmt.Print("Line 4 -a is either less than or equal to b\n") }

if (b>= a){

fmt.Print("Line 5 - b is either grater than or equal to b\n") } }

OUTPUT:

Line 1 - a is not equal to b

Line 2 - a is not less than b

Line 3 - a is greater than b

Line 4 - a is either less than or equal to b

Line 5 - b is either greater than or equal to b

Result

Thus the above program was successfully completed

4
EX.NO: 03 ​ ​ ​ ​ ​ ​ ​ NAME :

DATE: ​​ ​ ​ REG.NO:

Write a program to understand logical operations in Go.

Aim: To Write a program on logical operations in Go

Procedure

1.​ Understand what logical operations are: Logical operations are operations that take one or
more boolean values (true or false) and return a boolean result based on the rules of logic.
2.​ Familiarize yourself with the boolean data type in Go: In Go, the boolean data type
represents a boolean value, which can be either true or false.
3.​ Learn about the AND operator: The AND operator in Go is represented by the &&
symbol.
4.​ Learn about the OR operator: The OR operator in Go is represented by the || symbol.
5.​ Learn about the NOT operator: The NOT operator in Go is represented by the! Symbol.
6.​ Practice combining logical operators: You can combine logical operators to create more
complex expressions.

Program

package main

import "fmt"

func main() {

var a bool = true

var b bool = false

if ( a && b) {

fmt . Printf ("Line 1 - Condition is true\n" ) }

if ( a | | b ) {

fmt . Printf("Line 2 -condition is true\n" )

5
/* lets change the value of a and b */

a = false

b = true

if ( a && b ) {

fmt . Printf( Line 3 - Condition is true\n" )

} else {

fmt . Printf("Line 3 - Condition is not true\n") }

if ( ! ( a && b ) {

fmt . Printf(" Line 4 - Condition is true\n" )

OUTPUT:

Line 2 - Condition is true

Line 3 - Condition is not true

Line 4 - Condition is true

Result

Thus the above program was successfully completed

6
EX.NO: 04​ ​ ​ ​ ​ ​ ​ NAME :

DATE: 25.01.23​ ​ ​ ​ ​ ​ REG.NO:

Write a Go Program constants of character, string, Boolean, and numeric values.

Aim: To Write a Go Program constants of character, string, Boolean, and numeric values.

Procedure

1.​ Understand what constants


2.​ Declare a constant of a character
3.​ Declare a constant of a string
4.​ Declare a constant of a Boolean value
5.​ Declare a constant of a numeric value

Program

package main

import (

"fmt"

"math")

const s string = "constant"

func main() {

fmt.Println(s)

const n = 500000000

const d = 3e20 / n

fmt.Println(d)

fmt.Println(int64(d))

fmt.Println(math.Sin(n))}​

7
OUTPUT:

$ go run constant.go

constant

6e+11

600000000000

Result

Thus the above program was successfully completed

8
EX.NO: 05​ ​ ​ ​ ​ ​ ​ NAME :

DATE:01.02.23​ ​ ​ ​ ​ ​ REG.NO:

Write a Program for is Go’s only looping construct.

Aim: To Write a Program for is Go’s only looping construct.

Procedure

1.​ Understand what a "for" loop is


2.​ Declare the loop variable and the condition
3.​ Write the code to execute in the loop
4.​ Understand the different types of "for" loops
5.​ Use the "break" and "continue" statements
6.​ Understanding of how to use Go's only looping construct, the "for" loop.

Program

package main

import "fmt"

func main() {

i := 1

for i <= 3 {

fmt.Println(i)

i=i+1

for j := 7; j <= 9; j++ {

fmt.Println(j)

for {

9
fmt.Println("loop")

break

for n := 0; n <= 5; n++ {

if n%2 == 0 {

continue

fmt.Println(n)

OUTPUT:

$ go run for.go

loop

Result

Thus the above program was successfully completed

10
EX.NO : 06​ ​ ​ ​ ​ ​ ​ NAME :

DATE:08.02.23​ ​ ​ ​ ​ ​ REG.NO :

Write a GO Program Branching with if and else in Go is straight-forward.

Aim: To Write a GO Program Branching with if and else in Go is straight-forward.

Procedure

1.​ Understand what "if" and "else" statements


2.​ Declare the condition
3.​ Use "else" for the alternative block
4.​ Use "else if" for multiple conditions
5.​ Understand the order of execution
6.​ Understanding of how to use branching with "if" and "else" statements in Go.

Program

package main

import "fmt"

func main() {

if 7%2 == 0 {

fmt.Println("7 is even")

} else {

fmt.Println("7 is odd")

if 8%4 == 0 {

fmt.Println("8 is divisible by 4")

if num := 9; num < 0 {

11
fmt.Println(num, "is negative")

} else if num < 10 {

fmt.Println(num, "has 1 digit")

} else {

fmt.Println(num, "has multiple digits")

OUTPUT:

$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

Result

Thus the above program was successfully completed

12
EX.NO : 07​ ​ ​ ​ ​ ​ ​ NAME :

DATE:15.02.23​ ​ ​ ​ ​ ​ REG.NO:

Write a GO Program Switch statements express conditionals across many branches.

Aim: Write a GO Program Switch statements express conditionals across many branches.

Procedure

1.​ Understand what switch statements


2.​ Declare the expression
3.​ Understand how cases work
4.​ Understand how fall through works
5.​ Understanding of how to use switch statements in Go.

Program

package main

import (

"fmt"

"time"

func main() {

i := 2

fmt.Print("Write ", i, " as ")

switch i {

case 1:

fmt.Println("one")

case 2:

fmt.Println("two")

13
case 3:

fmt.Println("three")

switch time.Now().Weekday() {

case time.Saturday, time.Sunday:

fmt.Println("It's the weekend")

default:

fmt.Println("It's a weekday")

t := time.Now()

switch {

case t.Hour() < 12:

fmt.Println("It's before noon")

default:

fmt.Println("It's after noon")

whatAmI := func(i interface{}) {

switch t := i.(type) {

case bool:

fmt.Println("I'm a bool")

case int:

fmt.Println("I'm an int")

default:

fmt.Printf("Don't know type %T\n", t)

14
}

whatAmI(true)

whatAmI(1)

whatAmI("hey")

OUTPUT:

$ go run switch.go
Write 2 as two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type string

Result

Thus the above program was successfully completed

15
EX.NO : 08 ​ ​ ​ ​ ​ ​ NAME :

DATE:22.02.23​ ​ ​ ​ ​ ​ REG.NO:

Write a GO Program an array is a numbered sequence of elements of a specific length.

Aim: To Write a GO Program an array is a numbered sequence of elements of a specific length.

Procedure

1.​ Declare an array


2.​ Assign values to the array.
3.​ Access elements of the array.
4.​ Iterate over the array
5.​ Initialize an array
6.​ Understanding of how to work with arrays in Go.

Program

package main

import "fmt"

func main() {

var a [5]int

fmt.Println("emp:", a)

a[4] = 100

fmt.Println("set:", a)

fmt.Println("get:", a[4])

fmt.Println("len:", len(a))

b := [5]int{1, 2, 3, 4, 5}

fmt.Println("dcl:", b)

var twoD [2][3]int

16
for i := 0; i < 2; i++ {

for j := 0; j < 3; j++ {

twoD[i][j] = i + j

fmt.Println("2d: ", twoD)}

OUTPUT:

$ go run arrays.go

emp: [0 0 0 0 0]

set: [0 0 0 0 100]

get: 100

len: 5

dcl: [1 2 3 4 5]

2d: [[0 1 2] [1 2 3]]

Result

Thus the above program was successfully completed

17
EX.NO : 09​ ​ ​ ​ ​ ​ ​ NAME :

DATE:01.03.23​ ​ ​ ​ ​ ​ REG.NO:

Write a GO Program Slices are a key data type in go, giving a more powerful interface to
sequences than arrays.

Aim: To Write a GO Program Slices are a key data type in go, giving a more powerful interface
to sequences than arrays.

Procedure

1.​ Declare a slice variable


2.​ Add elements to the slice
3.​ Access elements in the slice
4.​ Create a new slice from a slice using the square bracket notation
5.​ Iterate over the slice

Program

package main

import "fmt"

func main() {

s := make([]string, 3)

fmt.Println("emp:", s)

s[0] = "a"

s[1] = "b"

s[2] = "c"

fmt.Println("set:", s)

fmt.Println("get:", s[2])

fmt.Println("len:", len(s))

18
s = append(s, "d")

s = append(s, "e", "f")

fmt.Println("apd:", s)

c := make([]string, len(s))

copy(c, s)

fmt.Println("cpy:", c)

l := s[2:5]

fmt.Println("sl1:", l)

l = s[:5]

fmt.Println("sl2:", l)

l = s[2:]

fmt.Println("sl3:", l)

t := []string{"g", "h", "i"}

fmt.Println("dcl:", t)

twoD := make([][]int, 3)

for i := 0; i < 3; i++ {

innerLen := i + 1

twoD[i] = make([]int, innerLen)

for j := 0; j < innerLen; j++ {

twoD[i][j] = i + j

fmt.Println("2d: ", twoD)

19
OUTPUT:

$ go run slices.go
emp: [ ]
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d: [[0] [1 2] [2 3 4]]

Result

Thus the above program was successfully completed

20
EX.NO : 10 ​ ​ ​ ​ ​ ​ NAME :

DATE: ​ ​ ​ ​ ​ ​ REG.NO:

Write a GO Program Maps are Go’s built-in associative data type.

Aim: To Write a GO Program Maps are Go’s built-in associative data type.

Procedure

1.​ Declare A Map Variable


2.​ Add Key-Value Pairs To The Map
3.​ Access Values In The Map
4.​ Modify Values In The Map
5.​ Check If A Key Exists In The Map
6.​ Delete Key-Value Pairs From The Map

Program

package main

import "fmt"

func main() {

m := make(map[string]int)

m["k1"] = 7

m["k2"] = 13

fmt.Println("map:", m)

v1 := m["k1"]

fmt.Println("v1: ", v1)

fmt.Println("len:", len(m))

delete(m, "k2")

fmt.Println("map:", m) _, prs := m["k2"]

21
fmt.Println("prs:", prs)

n := map[string]int{"foo": 1, "bar": 2}

fmt.Println("map:", n)

Output:

$ go run maps.go

map: map[k1:7 k2:13]

v1: 7

len: 2

map: map[k1:7]

prs: false

map: map[bar:2 foo:1]

Result

Thus the above program was successfully completed

22
EX.NO : 11 ​ ​ ​ ​ ​ ​ ​ NAME :

DATE: ​ ​ ​ ​ ​ ​ REG.NO :

Write a GO supports pointers, allowing you to pass references to values and records within
your program.

Aim: To Write a GO supports pointers, allowing you to pass references to values and records
within your program.

Procedure

1.​ Declare a variable and its pointer


2.​ Assign the pointer to the variable
3.​ Access the value of the variable through the pointer
4.​ Modify the value of the variable through the pointer
5.​ Pass a pointer to a function
6.​ Declare a pointer to a struct
7.​ Assign the pointer to the struct variable
8.​ Access the fields of the struct through the pointer
9.​ Modify the fields of the struct through the pointer

Program

package main

import "fmt"

func zeroval(ival int) {

ival = 0

func zeroptr(iptr *int) {

*iptr = 0

func main() {

23
i := 1

fmt.Println("initial:", i)

zeroval(i)

fmt.Println("zeroval:", i)

zeroptr(&i)

fmt.Println("zeroptr:", i)

fmt.Println("pointer:", &i)

OUTPUT:​
$ go run pointers.go

initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x42131100

Result

Thus the above program was successfully completed

24
EX.NO : 12​ ​ ​ ​ ​ ​ ​ NAME :

DATE: ​ ​ ​ ​ ​ ​ REG.NO :

Write a GO Program Variadic functions.

Aim: To Write a GO Program Variadic functions.

Procedure

1.​ Define a function with a parameter of type ...<type>. The three dots ... indicate that this
parameter can take any number of arguments of type <type>. For example:
2.​ In the function body, you can treat the args parameter like a slice of type <type>[].
3.​ Call the function with any number of arguments of type <type>. You can pass in zero,
one, or many arguments.
4.​ If you have other parameters in the function signature, the Variadic parameter must come
last.
5.​ You can also use the ... notation to pass a slice of type <type>[] as the Variadic argument.

Program

package main

import "fmt"

func sum(nums ...int) {

fmt.Print(nums, " ")

total := 0

for _, num := range nums {

total += num

fmt.Println(total)

func main() {

25
sum(1, 2)

sum(1, 2, 3)

nums := []int{1, 2, 3, 4}

sum(nums...)

OUPUT:​
$ go run variadic-functions.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

Result

Thus the above program was successfully completed

EX.NO : 13​ ​ ​ ​ ​ ​ ​ NAME :

26
DATE: ​ ​ ​ ​ ​ ​ REG.NO:

Write a Go supports recursive functions. Here’s a classic factorial example.

Aim: To Write a Go supports recursive functions.

Procedure

1.​ Define a function with a base case that stops the recursion.
2.​ Define a recursive case that calls the function itself with modified arguments.
3.​ Pass modified arguments to the recursive function call that moves towards the base case.
4.​ Make sure that each recursive call is a step closer to the base case.
5.​ Return the value from each recursive call.
6.​ Check that the function's input is valid before making the first recursive call.
7.​ Test the function by calling it with different inputs
8.​ Make sure it returns the expected result for each input.

Program

package main

import "fmt"

func fact(n int) int {

if n == 0 {

return 1

return n * fact(n-1)

func main() {

fmt.Println(fact(7))

27
OUTPUT:

$ go run recursion.go
5040

Result

Thus the above program was successfully completed

EX.NO : 14​ ​ ​ ​ ​ ​ ​ NAME :

28
DATE:​​ ​ ​ ​ ​ ​ REG.NO:

Write a Go by Example: Channel Buffering

Aim: To write a Go by Example: Channel Buffering

Procedure

1.​ Create a channel with a buffer size by including the buffer size as a second parameter
when calling make (chan <type>, <buffer size>).
2.​ Send values to the channel using the <-operator.
3.​ Receive values from the channel using the <- operator.
4.​ Use the len() function to determine the number of values currently in the buffer.
5.​ Use the cap () function to determine the capacity of the buffer.
6.​ Make sure to close the channel when you are done sending values.
7.​ This will signal to any receiving go routines that there will be no more values sent to the
channel.

Program

package main

import "fmt"

func main() {

messages := make(chan string, 2)

messages <- "buffered"

messages <- "channel"

fmt.Println(<-messages)

fmt.Println(<-messages)

OUTPUT:

29

$ go run channel-buffering .go

buffered

channel

Result

Thus the above program was successfully completed

30

You might also like