Professional Documents
Culture Documents
8. Write a function that accepts 2 numbers and performs addition, subtraction and
return both values.
9. Write a function with one variadic parameter that finds the greatest number in a
list of n numbers.
13. Input the string ‘Hello World’ and slice it into two
TABLE OF CONTENTS 1
1. Hello World Program
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Output:
>go run welcome.go
Hello World!
import "fmt"
func main() {
var a, b int
fmt.Println("Finds Sum, Product and Quotient")
fmt.Println("--------------------")
fmt.Print("Enter Two Numbers ")
fmt.Scanf("%d %d", &a, &b)
fmt.Printf("Sum : %d\n", a + b)
fmt.Printf("Product : %d\n", a * b)
fmt.Printf("Quotient : %d\n", a / b)
}
Output:
>go run welcome.go
--------------------
Sum : 6
Product : 8
Quotient : 0
import "fmt"
func main() {
fmt.Println( "Enter Number: ")
var n int
fmt.Scanln(&n)
if (n >=1 && n<=10) {
fmt.Println(n, "is between 1 and 10" )
}
else {
fmt.Println(n, "is out of range")
}
}
Output:
>go run Check.go
Enter Number: 12
12 is out of range
Enter Number: 4
4 is between 1 and 10
import "fmt"
func main() {
fmt.Print( "Enter Number: ")
var n int
fmt.Scanln(&n)
sum := 0
for i := 1; i <= n; i++ {
sum += i
}
fmt.Println(sum)
}
Output:
>go run sum1toN.go
Enter Number: 10
55
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
Output:
>go run slice.go
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
d len=3 cap=3 [0 0 0]
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"]
fmt.Println("prs:", prs)
n := map[string]int{"foo": 1, "bar": 2}
fmt.Println("map:", n)
}
Output:
go run map.go
v1: 7
len: 2
map: map[k1:7]
prs: false
map: map[bar:2 foo:1]
import "fmt"
func main() {
fmt.Println(factorial(3))
fmt.Println(factorial(4))
fmt.Println(factorial(5))
}
Output:
>go run fact.go
24
120
import "fmt"
func main() {
fmt.Println("Enter 2 Numbers: ")
fmt.Scanln(&x,&y)
fmt.Println(calc(x,y))
}
Output:
go run func.go
Enter 2 Numbers:
25 20
45 5
8. Write a function that accepts 2 numbers and performs addition, subtraction and return both values 1
9. Write a function with one
variadic parameter that finds
the greatest number in a list of
n numbers
package main
import "fmt"
func main() {
fmt.Println(greatestNumber(12,3,84,32,52,97,6))
}
Output:
>go run variadicfunc.go
97
9. Write a function with one variadic parameter that finds the greatest number in a list of n numbers 1
10. Write a program to swap
two integers
package main
import "fmt"
func main() {
fmt.Println(myfunction())
}
Output:
>go run swap.go
[5 10]
import "fmt"
func main() {
var a Address
fmt.Println(a)
a3 := Address{Name: "Delhi"}
fmt.Println("Address3: ", a3)
}
Output:
>go run structures.go
{ 0}
import "fmt"
func main() {
var t tank
t = myvalue{10, 14}
fmt.Println("Area of tank:", t.Tarea())
fmt.Println("Volume of tank:", t.Volume())
}
Output:
go run interfaces.go
import (
"fmt"
"strings"
)
func main() {
str1 := "Hello World"
fmt.Println("String 1: ", str1)
Output:
>go run HW_slice.go
String 1: Hello World
13. Input the string ‘Hello World’ and slice it into two 1
14. Program to write a list of
cities to a new file
// Golang program to read and write the files
package main
func CreateFile() {
fmt.Printf("Writing to a file in Go lang\n")
file, err := os.Create("test.txt")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
defer file.Close()
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
func ReadFile() {
fmt.Printf("\n\nReading a file in Go lang\n")
fileName := "test.txt"
data, err := ioutil.ReadFile("test.txt")
if err != nil {
log.Panicf("failed reading data from file: %s", err)
}
fmt.Printf("\nFile Name: %s", fileName)
fmt.Printf("\nSize: %d bytes", len(data))
fmt.Printf("\nData: %s", data)
// main function
func main() {
CreateFile()
Output:
>go run CityFile.go
Writing to a file in Go lang
Size: 34 bytes
Data: Chennai
Mumbai
Delhi
Trivandrum
import (
"io"
"log"
"net"
"time"
)
func main() {
listener, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err) // e.g., connection aborted
continue
}
go handleConn(conn) // handle one connection at a time
}
}
Client Program
package main
import (
"io"
"log"
func main() {
conn, err := net.Dial("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
mustCopy(os.Stdout, conn)
}
Output:
13:58:54 $ ./netcat1
13:58:55
13:58:56
^C
13:58:57
13:58:58
13:58:59
^C
import "fmt"
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
Output:
-5 17 12