How to create a function in Golang or Go?
Tags: go
Published: 2022-08-13 19:02:31

---
  title: How to create a function in Golang or Go?
  published: true
  tags: go
  date: Sun Aug 14 2022 00:32:31 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/create-function-golang-go
  cover_image: https://melvingeorge.me/_next/static/images/main-04052960baddb8a8e1de8940e69e4b8f.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/create-function-golang-go)
  
To create a function in Golang or Go, you have to write the keyword `func` then the name of the function followed by the `()` symbol (opening and closing brackets). Inside the brackets, we can have the parameters with the type associated after that. After the brackets, you can write the type of the return value.

### TL;DR

```golang
package main

import "fmt"

func main() {
    // call the `sayGreeting` function
    message := sayGreeting("John")
    fmt.Println(message)

    /*
        OUTPUT:

        John
        Hello World
    */
}

// a simple function that accepts
// a parameter called `personName` of type string
// and returns the string `Hello World`
func sayGreeting(personName string) string {
    fmt.Println(personName)
    return `Hello World`
}
```

For example, let's say we have to create a function called `sayGreeting` that returns the `string` called `Hello World`.

First, we can write the keyword `func` followed by the name of the function, in our case `sayGreeting` name.

It can be done like this,

```golang
package main

func main() {

}

// a simple function that
// returns the string `Hello World`
func sayGreeting() string {
    return `Hello World`
}
```

To call the function, we can write the name of the function followed by the `()` symbol (opening and closing brackets) inside the `main()` function.

It can be done like this,

```golang
package main

func main() {
    // call the `sayGreeting` function
    sayGreeting();
}

// a simple function that
// returns the string `Hello World`
func sayGreeting() string {
    return `Hello World`
}
```

Now let's save the return value of the `sayGreeting` function to a variable called message and then print it to the console using the `Println()` method from the `fmt` module.

It can be done like this,

```golang
package main

import "fmt"

func main() {
    // call the `sayGreeting` function
    message := sayGreeting()
    fmt.Println(message)

    /*
        OUTPUT:

        Hello World
    */
}

// a simple function that
// returns the string `Hello World`
func sayGreeting() string {
    return `Hello World`
}
```

Till now we haven't used the function parameters, let's create a parameter called `personName` with the type of `string` and then print it before returning the `Hello World` string.

It can be done like this,

```golang
package main

import "fmt"

func main() {
    // call the `sayGreeting` function
    message := sayGreeting("John")
    fmt.Println(message)

    /*
        OUTPUT:

        John
        Hello World
    */
}

// a simple function that accepts
// a parameter called `personName` of type string
// and returns the string `Hello World`
func sayGreeting(personName string) string {
    fmt.Println(personName)
    return `Hello World`
}
```

We have successfully created a function in Golang or Go. Yay 🥳!

See the above code live in [The Go Playground](https://go.dev/play/p/NyCZl4NDYNM).

That's all 😃!

### Feel free to share if you found this helpful 😃.

---