Go Record
Go Record
NO : 01 NAME :
DATE: REG.NO:
Procedure
Program
package main
import "fmt"
func main() {
var c int
c =a + b
c =a - b
c =a * b
c =a % b
a++
a--
OUTPUT:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
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
2
EX.NO : 02 NAME :
DATE: REG.NO:
Procedure
package main
import "fmt"
func main() {
if (a == b) {
}else {
3
if(a<b) {
}else {
} if (a>b){
}else {
a=5
b = 20
if (a <= b){
if (b>= a){
OUTPUT:
Result
4
EX.NO: 03 NAME :
DATE: REG.NO:
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() {
if ( a && b) {
if ( a | | b ) {
5
/* lets change the value of a and b */
a = false
b = true
if ( a && b ) {
} else {
if ( ! ( a && b ) {
OUTPUT:
Result
6
EX.NO: 04 NAME :
Aim: To Write a Go Program constants of character, string, Boolean, and numeric values.
Procedure
Program
package main
import (
"fmt"
"math")
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
8
EX.NO: 05 NAME :
DATE:01.02.23 REG.NO:
Procedure
Program
package main
import "fmt"
func main() {
i := 1
for i <= 3 {
fmt.Println(i)
i=i+1
fmt.Println(j)
for {
9
fmt.Println("loop")
break
if n%2 == 0 {
continue
fmt.Println(n)
OUTPUT:
$ go run for.go
loop
Result
10
EX.NO : 06 NAME :
DATE:08.02.23 REG.NO :
Procedure
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 {
11
fmt.Println(num, "is negative")
} else {
OUTPUT:
$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit
Result
12
EX.NO : 07 NAME :
DATE:15.02.23 REG.NO:
Aim: Write a GO Program Switch statements express conditionals across many branches.
Procedure
Program
package main
import (
"fmt"
"time"
func main() {
i := 2
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
13
case 3:
fmt.Println("three")
switch time.Now().Weekday() {
default:
fmt.Println("It's a weekday")
t := time.Now()
switch {
default:
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
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
15
EX.NO : 08 NAME :
DATE:22.02.23 REG.NO:
Procedure
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)
16
for i := 0; i < 2; i++ {
twoD[i][j] = i + j
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]
Result
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
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")
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)
fmt.Println("dcl:", t)
twoD := make([][]int, 3)
innerLen := i + 1
twoD[i][j] = i + j
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
20
EX.NO : 10 NAME :
DATE: REG.NO:
Aim: To Write a GO Program Maps are Go’s built-in associative data type.
Procedure
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("len:", len(m))
delete(m, "k2")
21
fmt.Println("prs:", prs)
n := map[string]int{"foo": 1, "bar": 2}
fmt.Println("map:", n)
Output:
$ go run maps.go
v1: 7
len: 2
map: map[k1:7]
prs: false
Result
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
Program
package main
import "fmt"
ival = 0
*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
24
EX.NO : 12 NAME :
DATE: REG.NO :
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"
total := 0
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
26
DATE: REG.NO:
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"
if n == 0 {
return 1
return n * fact(n-1)
func main() {
fmt.Println(fact(7))
27
OUTPUT:
$ go run recursion.go
5040
Result
28
DATE: REG.NO:
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() {
fmt.Println(<-messages)
fmt.Println(<-messages)
OUTPUT:
29
buffered
channel
Result
30