Go Language: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 269: Line 269:
=Functions And Methods=
=Functions And Methods=
==Functions==
==Functions==
===Basics===
No surprises here except the symbol name is first
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
func test(inPort int, inNumberOfReries) bool {
    return true
}
isDone = test(1701, 3)
</syntaxhighlight>
</syntaxhighlight>
===The Go Approach===
We return error in functions and check for nil. We can return multiple return values
<syntaxhighlight lang="go">
func test(inPort int, inNumberOfReries) (int, error) {
    return 300, nil
}
_,err = test(1701, 3)
fmt.Println(err)
</syntaxhighlight>
==Methods==
==Methods==
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:54, 19 August 2020

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

Collections

Arrays

With go all the elements must have the same type. The type is declared at the end. They are zero based. I.E. the first array is at index 0. You cannot increase the size dynamically.

var arr [3]int 
arr[0] = 1
arr[0] = 2
arr[0] = 3

// Better with initializer
arr2 := [3]int {1,2,3}

Slices

Introduction

The slice is a dynamic type which can be derived/linked from an array

arr2 := [3]int {1,2,3}

slice := arr[:]

arr[1] = 42
slice[2] = 27

// output [1,42,27]

Appending

We can create a slice from an anonymous array and append

slice := []int {1,2,3}

slice = append(slice, 4,42,27)

// output [1,2,3,4,42,27]

Ranges

We can slice a slice

slice := []int {1,2,3}

slice = append(slice, 4,42,27)

s2 := slice[1:] // Element 1-end
s3 := slice[:2] // Element 0-2 Exclusive 0,1 not 2
s4 := slice[1:2] // Element 1-2 Exclusive 1 not 2

Map

Maps are like dictionaries. They associate a key with a value in a collection

m := map[string]int {"foo":42,"foo2":42}
fmt.Println(m["foo"])

// Deleting uses delete function
delete(m, "foo")

Struct

Structs field definitions are fixed at compile time like c++ structs. They are defined using the word type. The fields are initialised to their zero value

// Define
type use struct {
    ID int
    FirstName string
    LastName string
}

// Declare
var myUser user

// Initialisation like a dictionary
// Add comma on last line for multi-line
u2 := user { 
    ID:1,
    FirstName: "Iain",
    LastName: "Wiseman",
}

// Assign
myUser.ID = 1
myUser.FirstName = "Iain"
myUser.LastName = "Wiseman"

Functions And Methods

Functions

Basics

No surprises here except the symbol name is first

func test(inPort int, inNumberOfReries) bool {

    return true
}
isDone = test(1701, 3)

The Go Approach

We return error in functions and check for nil. We can return multiple return values

func test(inPort int, inNumberOfReries) (int, error) {

    return 300, nil
}

_,err = test(1701, 3)
fmt.Println(err)

Methods