Command line arguments in Golang

PROGRAM

package main

import (
"fmt"
"os"
)

func main() {
all_args := os.Args

fmt.Println("Number of Arguments :", len(all_args))

fmt.Println("All Args except program :", all_args[1:])

fmt.Println("First Argument :", all_args[1])

fmt.Println("Second Argument :", all_args[2])

fmt.Println("Third Argument :", all_args[3])
}


OUTPUT

$ go build cmd_args.go

$ ./cmd_args fi1 sec2 thr3
Number of Arguments : 4
All Args except program : [fi1 sec2 thr3]
First Argument : fi1
Second Argument : sec2
Third Argument  : thr3

Structure in Golang

PROGRAM:

package main

import "fmt"

/* group the variable using struct */
type Rectange struct {
length int
width int
}

func Area(r Rectange) int {
return r.length * r.width
}

func Perimeter(r Rectange) int {
return 2 * (r.length * r.width)
}

func main() {
/* create a variable of type Rectangle */
var rect1 Rectange
/* set the individual field of Rectange type */
rect1.length = 5
rect1.width = 8
/* pass Rectangle type to function */
area := Area(rect1)
perimeter := Perimeter(rect1)
fmt.Println("Length-", rect1.length, " Width-", rect1.width,
" Area-", area, " Perimeter-", perimeter)

rect2 := Rectange{15, 30}
area = Area(rect2)
perimeter = Perimeter(rect2)
fmt.Println("Length-", rect2.length, " Width-", rect2.width,
" Area-", area, " Perimeter-", perimeter)}
}

OUTPUT:

Length- 5  Width- 8  Area- 40  Perimeter- 80
Length- 15  Width- 30  Area- 450  Perimeter- 900

Interface in Golang

PROGRAM:
package main

import (
"fmt"
"math"
)

type Geometry interface {
Area() float64
Perimeter() float64
}

type Rectangle struct {
length float64
width float64
}

type Circle struct {
radius float64
}

/* Rectangle type implementing Geometry interface */
func (r Rectangle) Area() float64 {
return r.length * r.width
}

func (r Rectangle) Perimeter() float64 {
return 2 * (r.length + r.width)
}

/* Circle type implementing Geometry interface */
func (c Circle) Area() float64 {
return math.Pi * c.radius * c.radius
}

//Circumference
func (c Circle) Perimeter() float64 {
return 2 * math.Pi * c.radius
}

func Calculate(g Geometry) {
area := g.Area()
perimeter := g.Perimeter()
fmt.Println("Area-", area, "Perimeter-", perimeter, "\n")
}

func main() {
/* create a variable of type Rectangle and initialize it */
rect := Rectangle{15.50, 30.25}
circ := Circle{4.2}

Calculate(rect)
Calculate(circ)
}

OUTPUT:
Area- 468.875 Perimeter- 91.5 

Area- 55.41769440932395 Perimeter- 26.389378290154262 

Defer in Golang

defer statement helps to execute program statement or anonymous function at end of the function execution.

Example Program:

package main

import "fmt"

func main() {
    fmt.Println("Start Main")
    testfunction()
    fmt.Println("End Main")
}

func testfunction() {
    /* This defer statement executed end of the function */
    defer fmt.Println("test function finished and exit")

    /* Do the work of funtion */
    var a, b int = 10, 20
    var c int = a + b 
    fmt.Println("Value of Addition", c)
}

Output:

Start Main
Value of Addition 30
test function finished and exit
End Main

One of the use case for defer statement is open a file for writing, next inside the defer mention file close then go for write. once function execution done defer will close the file automatically.

Panic in Golang


program:

package main

import "fmt"

func main() {
        fmt.Println("Before Function Foo Call")
        foo()
        fmt.Println("After Function Foo Call")
}

func foo() {
        fmt.Println("Before Panic")
        panic("foo function")
        fmt.Println("After Panic")

}


output:


Before Function Foo Call

Before Panic

panic: foo function

goroutine 1 [running]:
panic(0x4b8e00, 0xc82000a340)
/home/godir/go/src/runtime/panic.go:464 +0x3e6
main.foo()
        /home/godir/work/src/blog/example_panic/main.go:13 +0x11e
main.main()
        /home/godir/work/src/blog/example_panic/main.go:7 +0xe3