From 84dd8065bbcdc8dbbd2ad05eb59362cdcf9113be Mon Sep 17 00:00:00 2001 From: Michele Caci Date: Tue, 18 Jun 2024 18:38:34 +0200 Subject: [PATCH] Last update after test speech before pages check --- slides.md | 104 +++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 87 deletions(-) diff --git a/slides.md b/slides.md index ab7fc41..3c9a81e 100644 --- a/slides.md +++ b/slides.md @@ -36,10 +36,6 @@ mdc: true - - --- transition: fade-out layout: fact @@ -57,15 +53,11 @@ backgroundSize: 80% # Why is it important? -
+Striving for simplicity -As Go is simple to learn, you can adopt it for your everyday work __faster__ - -As Go is simple to write, you can write __clearer__ code - -As Go is simple to read, __anyone__ will be able to understand it __more easily__ +As a simple language it is __easier__ to adopt, __simpler__ to write and anyone with a basic knowledge will be able to jump into a Go codebase and understand it __faster__
@@ -74,40 +66,19 @@ __Leveraging the simplicity of Go is the key to improving our Go skills__
Let's see it with some examples
- - --- transition: fade-out --- # Example #1 -transferring data - - - -For example storing the content of a CSV file or transferring the body of an HTTP request - +Transferring data from a source to a target ````md magic-move {lines: true} ```go {all|2|2,6} -func forwardContentToTarget(target io.Writer, source io.Reader) error { +func forwardContentToTarget(source io.Reader, target io.Writer) error { bytes, err := io.ReadAll(source) // read the source if err != nil { return err @@ -118,7 +89,7 @@ func forwardContentToTarget(target io.Writer, source io.Reader) error { ``` ```go {2|all} -func forwardContentToTarget(target io.Writer, source io.Reader) error { +func forwardContentToTarget(source io.Reader, target io.Writer) error { _, err := io.Copy(target, source) // read the source and transfer it if err != nil { return err @@ -135,7 +106,7 @@ How about transferring the same content to multiple targets? ````md magic-move {lines: true} ```go {all|2|2,6-9} -func forwardContentToTargets(targets ...io.Writer, source io.Reader) error { +func forwardContentToTargets(source io.Reader, targets ...io.Writer) error { bytes, err := io.ReadAll(source) // read the source if err != nil { return err @@ -149,7 +120,7 @@ func forwardContentToTargets(targets ...io.Writer, source io.Reader) error { ``` ```go {3,5|all} -func forwardContentToTargets(targets ...io.Writer, source io.Reader) error { +func forwardContentToTargets(source io.Reader, targets ...io.Writer) error { // create a writer that includes all targets dsts := io.MultiWriter(targets) // read the source and transfer it @@ -167,9 +138,9 @@ transition: fade-out layout: lblue-fact --- -Gem #1: use io.Copy when transferring data +Gem #1: delegate the complexity of the code to the standard library -
use io.Read* when using that data
+
use io.Copy when transferring data
--- @@ -178,9 +149,7 @@ transition: fade-out # Example #2 -Error creation - -Let's see how errors can be created in Go code +Creating new errors ````md magic-move {lines: true} @@ -210,28 +179,7 @@ err := fmt.Errorf("an error occurred: %s", oErr.Error()) ---- -transition: fade-out -layout: lblue-fact ---- - -A human cares about the error message - - -
*a program cares more about what kind of error it is
-
- ---- -transition: fade-out ---- - -# Example #2 - -Error wrapping - -To our help, error wrapping was released with Go [v1.13](https://tip.golang.org/doc/go1.13#error_wrapping) (Sep 2019) - -And more recently, multi-error wrapping with Go [v1.20](https://tip.golang.org/doc/go1.20#errors) (Feb 2023) +To our help, error wrapping was released with Go [v1.13](https://tip.golang.org/doc/go1.13#error_wrapping) (Sep 2019) and multi-error wrapping with Go [v1.20](https://tip.golang.org/doc/go1.20#errors) (Feb 2023) ````md magic-move {lines: true} @@ -258,25 +206,15 @@ err := errors.Join(oErr1, oErr2, oErr3) ```` - - -By __wrapping__ an error you are creating a new error telling which kind it is - -By using __errors.As__ and __errors.Is__ you can test an error for what it may be instead of what kind of message it contains - - --- transition: fade-out layout: lblue-fact --- -Gem #2: wrap errors with %w or errors.Join +Gem #2: A human cares about the error message -
*changing from %[s|q|v] to %w is transparent
-
- -
most of the times
+
a program cares more about what kind of error it is
@@ -288,12 +226,10 @@ transition: fade-out # Example #3 -Slices and maps packages +Slices and maps packages (manipulating an http.Header) -As a _proxy server_, I want to manipulate an input header from an HTTP request by adding some custom headers and removing others - ````md magic-move {lines: true} ```go // http.Header is an alias for map[string][]string @@ -322,23 +258,17 @@ func update(h http.Header, toAdd http.Header, toDelete []string) { ``` ```` -Generics were released with Go [v1.21](https://tip.golang.org/doc/go1.21#library) (Aug 2023) - -In the same release, __maps__ and __slices__ packages were introduced to simplify operations made on them +__Maps__ and __slices__ packages were released with Go [v1.21](https://tip.golang.org/doc/go1.21#library) (Aug 2023) together with generics and use them to simplify maps and slices operations - - --- transition: fade-out layout: lblue-fact --- -Gem #3: use maps and slices packages +Gem #3: check new Go features as they released -
and reduce the complexity of the code dealing with them
+
and see how they can be used to simplify your code
---