All About Go (Part One)

A Brief History:
- 2007 developed by Robert Grieseme, Rob Pike, and Ken Thompson at Google
- 2009 became an open-source programming language
What is Go or “golang”?
- Go is a strong and statically typed language
Why was go built?
- Go was made with the goal of simplicity and readability: fewer features, easy to maintain, easy to work in & long term maintainability
- Clean procedural language designed for scalable software
- Composable distinct elements: concrete data types, functions & methods, interface, packages, concurrency
Getting Started:
- Go playground is a great place to get started

- Every go application is structured into packages. ‘main’ is used when developing an executable program.
- ‘import’ is used to import external libraries. “fmt” is the package that allows us to format strings.
- func main() {} is the entry point of our application. Here we will place the code we would like to run.
- Variables: variable declaration can be done inside or outside of the function call. Every time we initialize a variable, if it is not assigned, it has the values of zero. Variables values can be reassigned, however variables cannot be redeclared int the same scope. When a variable is redeclared, and overrides outer declared variable this is called shadowing (the variable with the inner most scope will take precedent). When a variable is declared, but not used this will lead to a compile time error. Lower case variables are scoped to the package. Upper case variables are exposed to outside package (any file in the package can access the variable).
- Best practice: variable length should allude to variable use, acronyms should be all uppercase
- Converting Variables: to convert data types you need to explicitly convert, to convert a string you must use the string conversion package
//Inside Function func main() {
var i int = 12
fmt.Println(i)
}OR func main() {
i := 12
fmt.Println(i)
}//Package Levelvar i int = 12
var s := = "Bob"or var (
s := "Bob"
i int = 12
)//Other data types
var (
var n bool = false //boolean
n:= 3.1456 //float
n:= 2.1E14 //float
var n complex64 = 1 + 2i //complex64
var n complex64 = complex(6, 8) //complex64
)
2. Constants: A constant has to be assignable at compile time. Constants are immutable, but can be shadowed. Operations can be done between constance and variables. Iota can be used with constance to create a list of enumerative constance, which will always start with zero.
func main() {
const myConst int = 2
}
Basic Data Types
Basic arithmetic operations are available in Go (+, -, *, /, %).When performing operation cannot convert data type or perform operations with data of different data types.
- Bool
- String (utf8 characters, can be converted back and forth to bytes )
- Int (int, int8, int16, int32, int64) negative or positive
- Uint (uint, unit8, unit16, unit32, unit64) always positive
- Byte (alias for unit8)
- Rune (alias for int32, represents a Unicode code point)
- Float (float32, float64)
- Complex (complex64, complex128)
Bitoperators:
- &: and operator copies a bit to the result if it exists in both operands.
- |: or operator copies a bit if it exists in either operand
- ^: exclusive or operator copies the bit if it is set in one operand but not both
- &^: and not operator copies a bit if it does not exists in either operand
https://www.tutorialspoint.com/go/go_bitwise_operators.htm
Array & Slice
An array is made up of contiguous lot of memory, and can only store data of the same data type. You can use the len function to interrogate the size of the array. If you move the array around the elements will move to another location in memory (if you make changes they will not reflect on copies). A slice, unlike an array, is dynamically-sized. To add elements to a slice, you use method append. Every slice is backed by an array, you can run cap function to check the capacity of the underlying array. You cannot append elements that exceed the capacity. If you modify a slice it will make changes to copies of the slice, since unlike an array copies of the slice point to the same location in memory.
func main() {
myArray := [...]int{5, 6, 7}
a := myArray[:] //slice all elements in array
b := myArray[:2] //slice the first element to the 2nd element
}
Map & Struct
In a map, unlike slice and array, order is not guaranteed. To check if an element is present in out map we enter ‘value, ok’ := m[‘key’]. A struct is used to hold on to data, fields are accessed using a dot notation, and pointers.
//Map
func main() {
myMap = map[string]int{
"Blue" : 5
}
myArray["Green"] = 6
delete(myMap, "Blue")
pop, ok := myArray["Green"]
}//Struct
func main() {
type MyStruct struct {
X int
Y int
}
fmt.Println(MyStruct{1, 2})
}
Interfaces
Describe behaviors, and store method definitions. Single method interface name should end with er. Any type that can have a method associated with it can implement an Interface. Some of the most popular interfaces include io.Writer, io.Reader, interface{}.
Best Practice:
- Using smaller interfaces is preferable
- Avoid exporting interfaces for types that will be consumed
package mainimport "fmt"func main() { //main function
myInt := IntCounter(0) //cast an integer to the IntCounter //create incrementer assign to myInt pointer
var inc Incrementer = &myInt
for i := 0; i < 10; i++ { //loop from 0-9
fmt.PrintIn(inc.Incrment())
}
}type Increment interface {
Increment() int //method defined(only returns an int)
}type IntCounter int //costume type defined as int
//implementation for Incrementer Interface
func (ic *IntCounter) Increment() int {
*ic++ //increment
return int(*ic) //return
}