Skip to main content

Functions and Methods of golang

 

Overview

In this blog i am going to talk about the functions and methods in golang. which is a important topic in go. if you have not read the previous blog or do not have a basic knowledge about go the i would suggest you to have a look at my other blog. 

Functions

you can declare a function as below. you don't need to declare return type unless your not returning something from your function. you get the return value in a way that is explained in the example code .
    func <function name >(<parameter> <parameter type>) (<return type>){}

Example Code :

    
    package main
    import ("fmt")

    func main() {

        test1()	// example 1

        sum := test2() // example 2
        fmt.Println("sum =",sum)

        sum2,error:= test3(30,40) // example 3
        fmt.Println("sum 2 =",sum2,error)

        sum3,_:= test3(80,60) // example 4 
        fmt.Println("sum 2 =",sum3)

        sum4,_:= test4(80,60.34) // example 5
        fmt.Println("sum 2 =",sum4)


    }


    func test1() {
        fmt.Println("hello from test 1")
    }

    func test2() int {
        sum := 20 + 30
        return sum
    }

    func test3(a,b int) (int,error){
        sum := a + b
        return sum,nil
    }

    func test4(a int,b float32) (float32,error){
        sum := float32(a) + b
        return sum,nil
    }

output :

    hello from test 1
    sum = 50
    sum 2 = 70 <nil>
    sum 2 = 140
    sum 2 = 140.34 
if you can see in test3 function we are grabbing the returned value by declaring the variable. In golang if you do not grab the value that is returning from the function then it will show an error. to avoid that error you need to fallow the test4 example of the function.

Methods

there is no classes in golang. instead of class they use struct and add a function to the struct so that it can be used as method. you can bind a method to a struct as below. 
    func (<variable name> <struct name >) <function name >
         (<parameter> <parameter type>) (<return type>){}
there is a sudden way you can use construct method in golang. it will be explaining in CRUD with golang blog.

 
Previous Blog Next Blog
Collection of golang Program flow of golang


Comments

Popular posts from this blog

CRUD with Golang

  Overview this blog will be about a  basic crud app with golang .   this app will provide API to store, update, delete information . you need to know the basics of golang to understand this project. if you do not have the basics knowledge  about golang I would suggest you to read my previous blog.  I have uploaded my code in  GitHub. click the link to clone or download this project  I am going to talk about the topic below. Project setup Data handling controller Project setup  Initiate mod and set up project as per golang. if you don't know how to setup golang project i would suggest you to have a look at my blog  Creating a project with golang . this is a really short blog. you will have a quick over view of how to setup a project with golang.  so now that you have setup the project then the folder structure will be as below. Data Handling data store, delete and update will be handled in the user module. code is given below  ...

Golang Commands

Overview    In this blog I am going to explain about basic go command and in depth knowledge about go commands will come after this series of basic go programming end. fallowing topic will be explained in this blog : primary commands help topic Commands Go ahead open command prompt on type go and press Enter. you can see all the commands and help topic that go provide. Primary Commands   this all are primary commands that go provide. this all are self explanatory. we have "bug" command for bug report, "clean" to clean previous projects, "install" to install packages to project.mod command needs explanation. which we will discuss on the next blog. but the command I want to talk about is doc. if we run ''go doc <package name>'' command  example "go doc json.decoder.decode" it will show the out put below. and if you run "go doc json.decoder" it will show the out put below. and if you run "go doc json" ...