# Overview of Functions in Go

In this lesson, we’ll walk through how to write a simple function in Go, covering the basics of functions, writing a "Hello World" program, understanding comments, and best practices for naming things in Go. You’ll also have the opportunity to try out a simple function yourself.

---

### Functions in Go

A function in Go is declared with the following basic structure:

```go
func functionName() {
    // function body
}
```

The parentheses `()` can include zero or more parameters, separated by commas. Each parameter must have a specified type.

#### The `main` Function

Every Go program starts with the `main` function. It's the entry point for the application. Without it, you'll encounter an error: `undefined: main.main`. The `main` function does not take any parameters and does not return a value. If you try to give it parameters or a return type, you’ll get another error: `func main must have no arguments and no return values`.

Here’s the syntax for declaring a function with parameters:

```go
func funcName(param1 type1, param2 type2) {
    // function body
}
```

If the function returns a value, the return type is specified after the parameter list:

```go
func funcName(param1 type1, param2 type2) type1 {
    // return something of type1
}
```

For multiple return values, it looks like this:

```go
func funcName(param1 type1, param2 type2) (ret1 type1, ret2 type2) {
    // function body
}
```

You can also write small, single-line functions:

```go
func Sum(a, b int) int { return a + b }
```

#### Writing the `main` Function

The `main` function acts as the starting point of your Go program. Let’s write a simple program that prints "Hello World".

### Hello World Program

Here’s a basic "Hello World" program in Go:

```go
package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}
```

In this example:

* We define the `main` package at the top.
    
* We import the `fmt` package to enable formatted I/O functions.
    
* Inside the `main` function, `fmt.Println("Hello World")` is used to print the message to the console.
    

### Comments in Go

Comments are non-executable lines of text in your code. They are meant to help explain the code and are ignored during compilation. Go supports two types of comments:

* Single-line comments start with `//`.
    
* Multi-line (block) comments are enclosed in `/* */`, but nesting block comments is not allowed.
    

Example:

```go
// This is a single-line comment

/*
This is a multi-line comment.
It can span multiple lines.
*/
```

### Naming Conventions in Go

Go emphasizes simplicity and readability. This means names should be short, clear, and descriptive. Here are a few guidelines for naming in Go:

* Avoid overly long or complex names.
    
* Don’t include the package name in your function or variable names.
    
* Use `MixedCaps` or `mixedCaps` for multi-word names instead of underscores.
    
* Functions that return an object should be named as nouns, without a `Get` prefix.
    
* For functions that modify an object, use a `SetName` format.
    

### Try It Yourself: Implementing a Simple Function

Now, it’s time for you to implement a basic function in Go. Let’s create a function that prints a message to the console. Here’s how:

1. Create a function named `PrintMessage` that accepts a parameter `message` of type `string`.
    
2. Inside the function, use `println` to output the `message`.
    

Here’s an example:

```go
package main

func PrintMessage(message string) {
    println(message)
}

func main() {
    PrintMessage("Hello, Go!")
}
```

This simple function takes a message as input and prints it to the console. You can modify it to print any message you want!

Try it out here - [https://go.dev/play/p/MZhPqlGKrcI](https://go.dev/play/p/MZhPqlGKrcI)

---

With this basic understanding of functions in Go, you’re now ready to explore more complex aspects of Go programming, starting with elementary data types.
