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.
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 mainindicates this program is executable. The code runs in themain()function. Other packages are intended as shared libraries, that is, shared files. These shared packages would not have themainpackage or function, and hence would not be executable. - The
importstatement imports thefmtpackage, a part of the Go standard library. Here, we will use it for printing our output viafmt.Println(). The "P" is capital becausePrintlnis exported from thefmtpackage, 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 syntaximport "fmt"and so on. - The
typekeyword defines a new type! Go has certain basic types, and you can create your own custom type. Here, we create an "Example" of typestruct. 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 float64andy float64denote the data fields expected in the Example type.xandyare simply variable names, whilefloat64is the data type. It is a number format (more information here). - The next part is the Add function.
-
funckeyword 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 bye, a variable) of typeExampleis 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.
-
-
float64describes the format of the value returned by the function, as described by thereturnstatement. -
e.xande.yrefer to the x and y fields of theeExample 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 = 7is 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 theAddfunction onnumbersdefined 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 output5 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 fmtcommand in the terminal:
go fmt /path/to/file
or alternatively:
gofmt -w file.goThe
golintlinter requires commenting throughout the code at various locations. You can use an alternate linter such asgolangci-lint(used for this program), which does not require you to do so.
Top comments (0)