From fa94e3fd5555ce4e723de0beeedc1dbc02a7f17f Mon Sep 17 00:00:00 2001 From: melaniegeraldine Date: Wed, 1 Mar 2023 18:15:05 +0100 Subject: [PATCH] feat: Go routines and channels --- foomo/docs/backend/go-by-example/channels.mdx | 0 .../go-by-example/cli-applications.mdx | 6 ++ foomo/docs/backend/go-by-example/context.mdx | 6 ++ foomo/docs/backend/go-by-example/embed.mdx | 6 ++ foomo/docs/backend/go-by-example/files.mdx | 6 ++ .../go-by-example/goroutines-and-channels.mdx | 76 +++++++++++++++++++ .../docs/backend/go-by-example/goroutines.mdx | 0 foomo/docs/backend/go-by-example/http.mdx | 6 ++ .../docs/backend/go-by-example/interfaces.mdx | 7 +- .../docs/backend/go-by-example/yaml-magic.mdx | 7 +- 10 files changed, 118 insertions(+), 2 deletions(-) delete mode 100644 foomo/docs/backend/go-by-example/channels.mdx create mode 100644 foomo/docs/backend/go-by-example/goroutines-and-channels.mdx delete mode 100644 foomo/docs/backend/go-by-example/goroutines.mdx diff --git a/foomo/docs/backend/go-by-example/channels.mdx b/foomo/docs/backend/go-by-example/channels.mdx deleted file mode 100644 index e69de29..0000000 diff --git a/foomo/docs/backend/go-by-example/cli-applications.mdx b/foomo/docs/backend/go-by-example/cli-applications.mdx index e69de29..23ec5ad 100644 --- a/foomo/docs/backend/go-by-example/cli-applications.mdx +++ b/foomo/docs/backend/go-by-example/cli-applications.mdx @@ -0,0 +1,6 @@ +--- +Title: CLI applications +position: 12 +--- + +# CLI applications \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/context.mdx b/foomo/docs/backend/go-by-example/context.mdx index 92f8cfd..1b03632 100644 --- a/foomo/docs/backend/go-by-example/context.mdx +++ b/foomo/docs/backend/go-by-example/context.mdx @@ -1 +1,7 @@ +--- +Title: Context +position: 10 +--- + +# Context https://gobyexample.com/context \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/embed.mdx b/foomo/docs/backend/go-by-example/embed.mdx index 3363b65..27a5d4f 100644 --- a/foomo/docs/backend/go-by-example/embed.mdx +++ b/foomo/docs/backend/go-by-example/embed.mdx @@ -1 +1,7 @@ +--- +Title: Embed +position: 13 +--- + +# Embed https://gobyexample.com/embed-directive \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/files.mdx b/foomo/docs/backend/go-by-example/files.mdx index e69de29..c718a9d 100644 --- a/foomo/docs/backend/go-by-example/files.mdx +++ b/foomo/docs/backend/go-by-example/files.mdx @@ -0,0 +1,6 @@ +--- +Title: Files +position: 14 +--- + +# Files \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/goroutines-and-channels.mdx b/foomo/docs/backend/go-by-example/goroutines-and-channels.mdx new file mode 100644 index 0000000..6a97572 --- /dev/null +++ b/foomo/docs/backend/go-by-example/goroutines-and-channels.mdx @@ -0,0 +1,76 @@ +--- +Title: Goroutines and Channels +position: 9 +--- + +import { GoPlayground } from '../../../src/components/GoPlayground'; + +# Go Routines and Channels + +## Go Routines +Go routines are go's mechanism to run code concurrently. +To create a goroutine we use the keyword ```go``` followed by a function invocation. +When you fork off a function by adding the ```go``` keyword before it, it will run asynchronous. So it might be the case that your main function returns before the routine is done executing + + + +If you run this example you see that sometimes the statements in the go routines are printed out but other times they are not. + +Go follows a fork-join concurrency model. Go routines are forked off the main function and the main function will continue to run. +In go you are responsible for implementing the join point. In the example we saw earlier we did not implement a join point. That means +we did not sync our go routines up with the main function and therefore a lot of times you did not see our statements being printed out. + +## Channels +Typically you will use a channel to communicate between go routines. We can have go routines reference the same place in memory +where a channel resides to be able to communicate with each other. This is a common way to implement a join point. + +A channel follows a First-In-First-Out (FIFO) structure. So the information you first write to the channel will be the first information to be read out from the channel. +Since the main function is a go routine as well it can also communicate with its child go routines through channels. + +### Unbuffered Channels +To make an unbuffered go channel call make with chan and specify the type of channel you want. + +```go +func main() { + myChannel := make(chan string) // unbuffered channel of type string +} +``` +To specify which function is writing and which function is reading from the channel we use arrows: <- + +```go +func main() { + myChannel := make(chan string) // unbuffered channel of type string + + go func() { + myChannel <- "I am always being printed now" // write to channel + }() + + data := <- myChannel // read from the channel and store it in data variable + fmt.Println(data) +} +``` +Unbuffered channels only allow go routines to communicate synchronously. This is because the go routine that sends the data to the unbuffered channel will +go into a waiting state once it has sent the data, until this data is read by the receiving go routine. +So until the data is read of the channel this go routine is blocked. + +### Buffered Channels +You can set a limit on the capacity of a channel by passing the limit to the make function: + +```go +func main() { + myChannel := make(chan string, 5) +} +``` + +To demonstrate how an unbuffered channel only allows for synchronous communication consider the following example: + + + +You see that even though we made both go routines wait when the channel was read, the go routine using the buffered channel printed out all of the statements at once. +And the go routine using the unbuffered channel had to wait each time until the channel was read out to be able to continue singing the greatest song of all time. \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/goroutines.mdx b/foomo/docs/backend/go-by-example/goroutines.mdx deleted file mode 100644 index e69de29..0000000 diff --git a/foomo/docs/backend/go-by-example/http.mdx b/foomo/docs/backend/go-by-example/http.mdx index e69de29..26f59e8 100644 --- a/foomo/docs/backend/go-by-example/http.mdx +++ b/foomo/docs/backend/go-by-example/http.mdx @@ -0,0 +1,6 @@ +--- +Title: HTTP Client and Server +position: 12 +--- + +# HTTP Client and Server \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/interfaces.mdx b/foomo/docs/backend/go-by-example/interfaces.mdx index 4d94406..83d3e1c 100644 --- a/foomo/docs/backend/go-by-example/interfaces.mdx +++ b/foomo/docs/backend/go-by-example/interfaces.mdx @@ -1 +1,6 @@ -todo \ No newline at end of file +--- +Title: Interfaces +position: 11 +--- + +# Interfaces \ No newline at end of file diff --git a/foomo/docs/backend/go-by-example/yaml-magic.mdx b/foomo/docs/backend/go-by-example/yaml-magic.mdx index bad5ef9..22b23bf 100644 --- a/foomo/docs/backend/go-by-example/yaml-magic.mdx +++ b/foomo/docs/backend/go-by-example/yaml-magic.mdx @@ -1,6 +1,11 @@ +--- +Title: Yaml +position: 16 +--- + import { GoPlayground } from '../../../src/components/GoPlayground'; -# yaml magic +# Yaml magic