Go for beginners: syntax, functions, and more
Tags: go, codenewbie
Published: 2021-01-05 16:00:30

Curious about Go?

New to Go?

Huh, Go what?

As described by Wikipedia, "Go is a statically typed, compiled programming language designed at Google". Apparently, it is syntactically similar to C, but as I do not know C (or any other compiled language for that matter),  it is completely new to me. In this series, I will break down certain features of the language. First up: basic syntax, packages, functions, variables and structs.

Let's begin with the following program to add two numbers.

```go
package main

import (
	"fmt"
)

type Example struct {
	x float64
	y float64
}

func Add(e Example) float64 {
	return e.x + e.y
}

var numbers = Example{2, 3}
var number2 float64 = 7

func main() {
	answer := Add(numbers)
	fmt.Println(answer, number2)
}
```

Let's break it down line-by-line:
- `package main` indicates this program is executable. The code runs in the `main()` function. Other packages are intended as shared libraries, that is, shared files. These shared packages would not have the `main` package or function, and hence would not be executable.
- The `import` statement imports the `fmt` package, a part of the Go standard library. Here, we will use it for printing our output via `fmt.Println()`. The "P" is capital because `Println` is exported from the `fmt` package, and exported names in Go are capitalized. As we have imported this package, we can only refer to the exported names. The bracket syntax is useful to import multiple packages, but individual packages can be imported using the syntax `import "fmt"` and so on.
- The `type` keyword defines a new type! Go has [certain basic types](https://tour.golang.org/basics/11), and you can create your own custom type. Here, we create an "Example" of type `struct`. A struct is simply a user defined type comprised of a collection of fields. I like to think of this as creating a custom data structure as required. It can contain various data types, such as strings, integers, and even other structs! 
- `x float64` and `y float64` denote the data fields expected in the Example type. `x` and `y` are simply variable names, while `float64` is the data type. It is a number format ([more information here](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)). 
- The next part is the Add function. 
    + `func` keyword describes the function named "Add". 
    + `(e Example)` describes the argument to be provided to the function and its type, that is, 1 argument (here indicated by `e`, a variable) of type `Example` is needed. Multiple parameters can be separated by commas, and if consecutive parameters are of the same type, the type can be given at the end of the arguments. Thus:
        - `(text string, number int)` requires, in order, a string and an integer argument.
        - `(a int, b int)` and `(a, b int)` both are equivalent, requiring 2 integer arguments.
    + `float64` describes the format of the value returned by the function, as described by the `return` statement.
    + `e.x` and `e.y` refer to the x and y fields of the `e` Example type. Thus, dot notation is used to refer to individual fields of a struct by their name.
- `var numbers = Example{2, 3}` declares a variable of name "numbers", which is equal to an Example type with values x=2 and y=3.
- `var number2 float64 = 7` is another example of a variable declaration, where "number2" is equal to 7 and its type is float64.
- `func main()` is the part of the program that is executed after compilation.
    + `answer := Add(numbers)` calls the `Add` function on `numbers` defined earlier, and assigns this value to the variable "answer". `:=` is shorthand notation for variable declaration that does not require the "var" keyword or a particular type, with its type being implicit. However, this notation is only available inside functions.
    + Finally, `fmt.Println(answer, number2)` prints out our two variables, giving us the output `5 7`!

And there you have it! Although a simpler function could be used for adding two numbers, I wrote it this way to include structs. 

Next up, methods and interfaces!

Happy coding!

**Notes:**

- Standard formatting of a Go program file can be done using the `go fmt` command in the terminal:
`go fmt /path/to/file`
or alternatively:
`gofmt -w file.go`

- The `golint` linter requires commenting throughout the code at various locations. You can use an alternate linter such as `golangci-lint` (used for this program), which does not require you to do so.