admin

Author


Concurrency

Concurrency issues
Many issues, of course, but Go tries to take care of them. Channel send and receive are atomic. The select statement is very carefully defined and implemented, etc.
But goroutines run in shared memory, communication networks can deadlock, multithreaded debuggers suck, and so on.
Go gives you the primitives
Don’t program the way you would in C [...]

Multiplexing

Channels are first-class values, which means they can be sent over channels. This property makes it easy to write a service multiplexer since the client can supply, along with its request, the channel on which to reply.

chanOfChans := make(chan chan int)
Or more typically
type Reply struct { … }
type Request struct {
arg1, arg2, arg3 some_type;
replyc chan [...]

Select

Select is a control structure in Go analogous to a communications switch statement. Each case must be a communication, either send or receive.

var c1, c2 chan int;
select {
case v :=

Channels in Go

Unless two goroutines can communicate, they can’t coordinate.
Go has a type called a channel that provides communication and synchronization capabilities.
It also has special control structures that build on channels to make concurrent programming easy.
The channel type

In its simplest form the type looks like this:
chan element_type
Given a value of this type, you can send and receive [...]

Goroutines

Terminology:

There are many terms for “things that run concurrently” – process, thread, coroutine, POSIX thread, NPTL thread, lightweight process, …,
But these all mean slightly different things. None means exactly how Go does concurrency. So we introduce a new term: goroutine.
Definition
A goroutine is a Go function or method executing concurrently in the same address space as [...]

Post Pic

Go – Defer

The defer statement executes a function (or method) when the enclosing function returns. The arguments are evaluated at the point of the defer; the function call happens upon return.
func data(name string) string {
f := os.Open(name, os.O_RDONLY, 0);
defer f.Close();
contents := io.ReadAll(f);
return contents;
}
Useful for closing fds, unlocking mutexes, etc.
Tracing with defer
func trace(s string) { Print(”entering:”, s); }
func [...]

Post Pic

Go – Functions

Functions
Functions are introduced by the func keyword.
Return type, if any, comes after parameters. The return does as you expect.
func square(f float) float { return f*f }
A function can return multiple values. If so, the return types are a parenthesized list.
func MySqrt(f float) (float, bool) {
if f >= 0 { return math.Sqrt(f), true }
return 0, false
}
Functions [...]

GO Programming Language Declarations

Declarations
Declarations are introduced by a keyword
var
const
type
func
and look reversed compared to C:
var i int
const PI = 22./7.
type Point struct { x, y int }
func sum(a, b int) int { return a + b }
Why are they reversed? Earlier example:
var p, q *int
Also functions read better and are consistent with other declarations.
Var
Variable declarations are introduced by var.
They [...]

Go Language Videos

Go Programming Language Promo

The Go Programming Language

Go Language Expressions

Expressions
Mostly C-like operators.
Binary operators:

Prec

operators

comments

6

* / % << >> & &^

&^ is “bit clear”

5

+ – | ^

^ is “xor”

4

== != < <= > >=

3

<-

communication

2

&&

1

||

Operators that are also unary: & ! * + – ^ <-
Unary ^ is complement.

Go vs. C expressions
Surprises for the C programmer:

fewer precedence levels (should be easy)
^ instead of ~ (it’s [...]

Page 1 of 212»