golang basics- things i missed

g

Well sure, I missed many. But I believe you already had the foundation to learn anything new on your own. Its impossible to cover everything. In fact I don’t even know I’ll cover all the good and interesting things in GO

But I promise you I’ll cover everything that is fundamental ‘for you to learn anything new’

So these are the random topics that I thought should be interesting to know.

  • interfaces
  • error handling

interfaces

If you already know Java, this should be familiar to you. But for others, think of it like

You’re planning on building a house. For a house, it should have atleast one room and one kitchen. This like a bare minimum necessity and like a blueprint for building a house. Now you’ll implement this house.

Thats the basic idea.

First the syntax for creating such a interface is:

type interface_name interface {
   method_name_1 [return_type]
   method_name_2 [return_type]
   ...
   method_name_n [return_type]
}
//[] means optional
  1. So first define such an interface.
type House interface{
	room() 
	kitchen() 
}

2. Define a struct

type Home struct{

}

3. Implement those functions

func(obj Home) room() {
	fmt.Println("room!")
}

func(obj Home) kitchen() {
	fmt.Println("kitchen!")
}

4. Use it


	home := Home{}

	home.kitchen()
	home.room()

Here’s the full code:

package main

import "fmt"

type House interface{
	room() 
	kitchen() 
}

type Home struct{

}

func(obj Home) room() {
	fmt.Println("room!")
}

func(obj Home) kitchen() {
	fmt.Println("kitchen!")
}

func main(){

	home := Home{}

	home.kitchen()
	home.room()

}

Now try to remove this function

func(obj Home) kitchen() {
	fmt.Println("kitchen!")
}

Now it should throw an error.

package main

import "fmt"

type House interface{
	room() 
	kitchen() 
}

type Home struct{

}

func(obj Home) room() {
	fmt.Println("room!")
}



func main(){

	home := Home{}

	home.kitchen()
	home.room()

}

As I said for you to create an object of a class ‘Home’, it should have both room and a kitchen

And thats it. thats the basic idea of interfaces.

Error handling

Lets go to the basics. Take this code:

package main

import "fmt"

func main(){

	fmt.Println(4/2)
}

Ah! we know this one. Its just a simple division. what could go wrong here? right?

Now look at this

package main

import "fmt"

func main(){

	fmt.Println(4/0)
}

Ok. we know what happened here. But thats not the problem. It throwed an error, it means the code below it won’t work. see for yourself.

see ‘finished’ is not printed. So we somehow need to handle it.

First lets put that inside a function

func divide(a int, b int) int{
	return a/b
}

Now lets run it

package main

import "fmt"

func divide(a int, b int) int{
	return a/b
}

func main(){

	fmt.Println(divide(4,2))
	fmt.Println(divide(4,0))

}

Now lets use some conditonal logic

package main

import "fmt"
import "errors"
			 

func divide(a int, b int) (int, error){
	if b==0{
		return 0, errors.New("what are you doing? b is zero")
	}
	return a/b, nil
}

func main(){
	val, err := divide(4,2)
	if  err != nil{
		fmt.Println(err)
	}else{
		fmt.Println(val)
	}

	val, err = divide(4,0)
	
	if  err != nil{
		fmt.Println(err)
	}else{
		fmt.Println(val)
	}

}

(Note: errors.New() is used to create our own errors)

And Thats basically it.

I’ll cover concurrency on my next article.

Happy coding!

About the author

vigneshwar

Add comment

Leave a Reply

By vigneshwar

Most common tags

%d bloggers like this: