Go Language

From bibbleWiki
Revision as of 07:27, 18 August 2020 by Iwiseman (talk | contribs) (Constants)
Jump to navigation Jump to search

Introduction

Characteristics

Google was using c++, java and python.

  • c++ high performance, type safety, slow compile, complex
  • java rapid compile, type safety, complicated eco system
  • python easy to use, lack of type safety, slow

So Go was created which had

  • Fast compilation
  • Fully compiled, better performance
  • Strongly typed
  • Concurrent by default, threaded
  • Garbage collected
  • Simplicity as a core value, for example Garbage collected, Strongly typed.

Whats Go Good At?

Good

  • Go is easy to learn
  • Easy concurrent programming with goroutines and channels
  • Great standard library
  • Go is performant
  • Language defined source code format
  • Standardized test framework
  • Go programs are great for operations
  • Defer statement, to avoid forgetting to clean up
  • New types

Bad

  • Go ignored advances in modern language design
  • Interfaces are structural types
  • Interface methods don't support default implementations
  • No enumerations
  • The := / var dilemma
  • Zero values that panic
  • Go doesn't have exceptions. Oh wait... it does!

Ugly

  • The dependency management nightmare
  • Mutability is hardcoded in the language
  • Slice gotchas
  • Mutability and channels: race conditions made easy
  • Noisy error management
  • Nil interface values
  • Struct field tags: runtime DSL in a string
  • No generics... at least not for you
  • Go has few data structures beyond slice and map
  • go generate: ok-ish, but...

Hello World

package main

import (
	"fmt"
)

// Comments are here
func main() {
	fmt.Println("Hello World")
}

Modules

Modules are files for controlling a project. To create a module

go mod init github.com/bibble235/webservice

To run our code

go run github.com/bibble235/webservice

Data Types

Boolean types

They are boolean types and consists of the two predefined constants: (a) true (b) false

Numeric types

They are again arithmetic types and they represents a) integer types or b) floating point values throughout the program.

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

Other Numeric types

  • fbyte same as uint8
  • frune same as int32
  • fuint 32 or 64 bits
  • fint same size as uint
  • uintptr an unsigned integer to store the uninterpreted bits of a pointer value

String types

A string type represents the set of string values. Its value is a sequence of bytes. Strings are immutable types that is once created, it is not possible to change the contents of a string. The predeclared string type is string.

Derived types

They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types f) Slice types g) Interface types h) Map types i) Channel Types

Declaring

There are 3 way to declare variables

// explicit
var i int

// explicit and assign
var i = 42

// Implicit
firstname := "Iain"

Pointers

We can create pointers with the "*"

var firstName *string // Nil

Assigning pointers is the same as c++ however it does not support pointer arithmathic

var firstName *string = new(string) // Nil
*firstName = "Arthur"
fmt.Println(*firstName)

Dereferencing is the same as c++ too.

var firstName *string // Nil

Assigning pointers is the same as c++ however it does not support pointer arithmathic

firstName := "Arthur"
ptr := &firstName

Constants

Introduction

Constant are available in go.

const pi = 3.1415

// types are not explicit so
const c = 3
fmt.Println(c + 1.2)
// Will work because there is not type and 1.2 can be added to c if c is deemed a float at runtime.

Iota

We can create consts like so

const (
    fred1 := "fred1" 
    fred2 := 2 
)

Using iota we can we can increment the values using an expression. e.g.

const (
    fred1 = iota
    fred2 = iota
)

// Outputs 0, 1

But we can put expression in this.

const (
    fred1 = iota + 6
    fred2 = 2 << itoa
)
// Outputs 6, 4

Omitting the expression after first declaration will mean the first expression is repeated. Iota is scoped to the block of code

const (
    fred1 = iota + 6
    fred2
)
// Outputs 6, 7