Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Golang - A drive through

Golang - A drive through

I recently took a course on Golang and learnt about the awesomeness and use of the language. In this talk I discuss the reasons why developers should consider learning Golang, the general language syntax and the interesting Go routines feature of the language. I also highlight resources that would get developers started if they are interested in learning.

Presented at Cotta & Cush Tech Talks
Tech Talks is a weekly internal event at Cotta & Cush Limited featuring technical presentations, app reviews among other discussions

Adeyemi Olaoye

June 22, 2018
Tweet

More Decks by Adeyemi Olaoye

Other Decks in Programming

Transcript

  1. Golang - A drive through Cotta&Cush Tech Talks, June 2018

    Adeyemi Olaoye Cotta&Cush Limited @yemexx1
  2. Job list Why Go? 01 General Syntax Structs & Interfaces

    Go Routines Getting Started Questions 02 03 04 05 06
  3. Moore’s Law is failing • (↑)Transistors = $$$ = quirks

    • (↓) Frequency • (↓) Performance • (↑) Cores = $$$ • (↑) Cache =
  4. { Yes, Go! Go.Pros - Go’s goroutines; concurrency - Neat

    and easy syntax - Little memory imprint - Similar in syntax to modern languages - Portable - single executable - Low level interfaces - Open Source
  5. Go is an open source programming language that enables the

    production of simple, efficient and reliable software at scale.
  6. Basic Syntax Comments // This is a single line comment

    /* This is a multi line Comment */ Literals 87 // integer literal 4.2313 // floating point “Hello World!” // string Statement Delimiters ; are allowed but considered redundant
  7. Data Types Integer Types uint8 - Unsigned 8-bit integers (0

    to 255) uint16 - Unsigned 16-bit integers (0 to 65535) uint32 - Unsigned 32-bit integers (0 to 4294967295) uint64 - Unsigned 64-bit integers (0 to 18446744073709551615) int8 - Signed 8-bit integers (-128 to 127) int16 - Signed 16-bit integers (-32768 to 32767) int32 - Signed 32-bit integers (-2147483648 to 2147483647) int64 - Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) Floating Types float32 - IEEE-754 32-bit floating-point numbers float64 - IEEE-754 64-bit floating-point numbers complex64 - Complex numbers with float32 real and imaginary parts complex128 - Complex numbers with float64 real and imaginary parts Others byte, rune, uint, int
  8. Packages, Imports ----- package main import “fmt” ----- package main

    import ( “fmt” “log” “me/mypackage” )
  9. Declaration By inference x := 2 Explicitly // can be

    declared outside package functions var x uint8 = 2 Constants // can be declared inside/outside package functions const REDIS_PORT = 6379 ----- package main import “fmt” var y uint32 = 50 const MAN = “Not hot!” func main() { x := “Hello World!” fmt.Println(y) fmt.Println(x) fmt.Println(MAN) } ----- 50 Hello World! Not Hot!
  10. Functions Keyword func Arguments (optional) <argument_name> <argument_type> Returns (optional) <return_name>(optional)

    <return_type> Format func (<arg_name> <arg_type>,...) (<ret_name> <ret_type>,...) { // function body } Others Named return, multiple return, variadic arguments, package exposure ----- package main import "fmt" func sayHello() { fmt.Println("Hello World!") } func SayHelloTo(name string){ fmt.Println("Hello", name + "!") } func getGreetingFor(name string) string { return "Hello " + name + "!" } func getAnotherGreetingFor(name string) (greeting string) { greeting = "Hello " + name + "!!" return } func main() { sayHello() SayHelloTo("Gopher") fmt.Println(getGreetingFor("Gopher")) fmt.Println(getAnotherGreetingFor("Gopher")) } ----- Hello World! Hello Gopher! Hello Gopher! Hello Gopher!!
  11. Arrays & Slices Arrays x := [length]<type>{<value>,...} or var x

    = [length]<type> Slices slice := make([]<type>, length) or slice := array[x:y] Examples x := [4]int{0,1,2,3} var x [4]int // initialized with default values x[0] = 2 slice := make([]string, 4) another_slice := x[1:3] ----- package main import "fmt" func main() { x := [4]int{0, 1, 2, 3} var y [4]int y[0] = 2 fmt.Println(x) fmt.Println(y) fmt.Println(x[1:3]) } ----- [0 1 2 3] [2 0 0 0] [1 2]
  12. Conditionals If Else if <conditional> { //.... } else if

    <conditional> { //... } or if <expression>; <conditional> { //.... } else if <expression>; <conditional> { //.... } else { // .... } ----- package main import "fmt" func main() { if 7%2 == 0 { fmt.Println("7 is even") } else { fmt.Println("7 is odd") } if 8%4 == 0 { fmt.Println("8 is divisible by 4") } if num := 9; num < 0 { fmt.Println(num, "is negative") } else if num < 10 { fmt.Println(num, "has 1 digit") } else { fmt.Println(num, "has multiple digits") } } ----- 7 is odd 8 is divisible by 4 9 has 1 digit
  13. Loops Keywords for, continue, break ----- package main import “fmt”

    func main() { i := 1 for i <= 3 { fmt.Println(i) i = i + 1 } for { fmt.Println(“hello”) break } for n := 0; n <= 5; n++ { if n%2 == 0 { continue } fmt.Println(n) } } ----
  14. Structs 02. A SIMPLE TYPE SYSTEM type Person struct {

    firstname string, lastname string, age int } person := Person{ firstname: “Adeyemi”, lastname: “Olaoye”, age: 19 } Typed collection of fields
  15. Methods 02. A SIMPLE TYPE SYSTEM func (p Person) GetFullName()

    string { return p.firstname + ‘ ’ + p.lastname } person := Person { firstname: “Adeyemi”, lastname: “Olaoye”, age: 19 } fmt.Println(person.GetFullName()) // Adeyemi Olaoye Structs can have methods
  16. type Printer interface { print() string } type Animal struct

    { name, type string } func (p Person) print() { return “Person: ” + p.firstname + p.lastname } func (a Animal) print() { return “Animal: ” + a.type + a.name } // Interfaces Interfaces in Go are quite flexible
  17. Go Routines package main import "fmt" func f(from string) {

    for i := 0; i < 3; i++ { fmt.Println(from, ":", i) } } func main() { f("direct") go f("goroutine") go func(msg string) { fmt.Println(msg) }("going") fmt.Scanln() fmt.Println("done") } Go routines are lightweight threads of execution Output ------ direct : 0 direct : 1 direct : 2 goroutine : 0 going goroutine : 1 goroutine : 2 <enter> done
  18. Getting Started - @golangweekly - @GolangGo - GDG Golang Berlin

    - Go Docs - godoc.org - Go Website - golang.org - Go Blog - blog.golang.org - Go Playground - play.golang.org - Short Go Course - gobyexample.com - Go Book - https://www.golang-book.com/