Brain Dump

GoLang

GoLang

Updated at: 04 Aug 2019 13:52:42

Basic Syntax Ideas

  1. Uses tokens. There are four classes: identifiers, keywords, operators, punctutation, and literals.

  2. Uses semicolons as terminators. However, Go programs may omit these semicolons following some rules.

  3. Common types: ArrayType, StructType, PointerType, FunctionType, SliceType, MapType, ChannelType

  4. Initialization of variables

Packages

The first statement in a Go source file must be:

package name

Basic Data Strutures

Functions

-support for multiple return values. (and hence, multiple assignment) -variadic functions

Iteration

for index, ele := range sliceName {} for i := 0; i < len(sliceName) ; i++ {}

Pointers

Go supports pointers, allowing you to pass references to values and records within your program.

Structs

Methods

Go support methods on struct types.

Interfaces

Named collections of method signatures. 1. It is a set of methods 2. It is a type

Core concept in Go’s type system: Instead of designing our abstractions in terms of what kind of data it holds, we design abstractons based on what actions our types can execute.

The interface{} type, the empty interface, is the source of much confusion. The interface{} type is the interface that has no methods. Since there is no implements keyword, all types implement at least zero methods, and satisfying an interface is done automatically, all types satisfy the empty interface. That means that if you write a function that takes an interface{} value as a parameter, you can supply that function with any value.

Miscellaneous Ideas

Handling Timeouts

SetDeadline: Deadlines are not timeouts. Once set they stay in force forever no matter if and how the connection is used. Call set deadline before each and every read/write operation.

All timeouts are implemented in terms of deadlines. They do not reset every time data is sent or received.

Critical for HTTP server that is exposed to the internet to enforce timeouts on client connections, else slow or disappearing clients might leak file descriptors.

ReadTimeout covers the time from when connection is accepted to when the request body is fully read. Implemented using SetReadDeadline immediately after accept.

Context Pattern

Each incoming request is handled as its own goroutine. Request handlers might spawn additional goroutines to access identity specific values but if the request is cancelled or times out, all goroutines working on request should exit quickly. Context package makes it easy to pass request scoped values, cancellation signals, and deadlines available as a context.

On interfaces and implementing methods

golang interface method requires pointer receiver https://stackoverflow.com/questions/40823315/x-does-not-implement-y-method-has-a-pointer-receiver

Method Sets

A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing embedded fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

Useful Links

https://queue.acm.org/detail.cfm?id=2839461 (*) https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/

https://stackoverflow.com/questions/32815400/how-to-perform-division-in-go