|
| 1 | +# The Globus BB Gophers Meetup |
| 2 | + |
| 3 | +Cyrill Schumacher |
| 4 | +Magazine zum Globus AG |
| 5 | + |
| 6 | +## Welcome |
| 7 | + |
| 8 | +Thank you for joining! |
| 9 | + |
| 10 | +.image 2017-08-10/GoCommunity.png _ 700 |
| 11 | +.caption _Gopher_ by [[http://www.reneefrench.com][Renée French]] |
| 12 | +.caption _Graphics_ by [[https://github.com/ashleymcnamara][Ashley McNamara]] |
| 13 | + |
| 14 | +## Let's go! |
| 15 | + |
| 16 | +.image 2017-08-10/Unicorn_Gopher.png _ 400 |
| 17 | +.caption _Gopher_ by [[http://www.reneefrench.com][Renée French]] |
| 18 | +.caption _Graphics_ by [[https://github.com/ashleymcnamara][Ashley McNamara]] |
| 19 | + |
| 20 | +## Go 1.18 main new features |
| 21 | + |
| 22 | +- new support for generic code using parameterized types. |
| 23 | +- adds built-in support for writing fuzzing-based tests, to automatically find |
| 24 | + inputs that cause your program to crash or return invalid answers. |
| 25 | +- adds a new “Go workspace mode”, which lets you work with multiple Go modules |
| 26 | + simultaneously, an important use case for larger projects. |
| 27 | +- contains an expanded go version -m command, which now records build details |
| 28 | + such as compiler flags. A program can query its own build details using |
| 29 | + debug.ReadBuildInfo, and it can now read build details from other binaries using |
| 30 | + the new debug/buildinfo package. This functionality is meant to be the foundation |
| 31 | + for any tool that needs to produce a software bill of materials (SBOM) for Go |
| 32 | + binaries. |
| 33 | + |
| 34 | +## Fuzzing |
| 35 | + |
| 36 | +.link https://go.dev/blog/fuzz-beta |
| 37 | +Fuzzing is a type of automated testing which continuously manipulates inputs to a |
| 38 | +program to find issues such as panics or bugs. These semi-random data mutations |
| 39 | +can discover new code coverage that existing unit tests may miss, and uncover |
| 40 | +edge case bugs which would otherwise go unnoticed. |
| 41 | + |
| 42 | +.code 2021-02-04/parse_fuzz.go |
| 43 | + |
| 44 | + |
| 45 | +## Workspace mode |
| 46 | + |
| 47 | +.link https://go.googlesource.com/proposal/+/master/design/45713-workspace.md |
| 48 | +A new workspace mode in the go command for editing multiple modules has been |
| 49 | +added. The presence of a go.work file in the working directory or a containing |
| 50 | +directory will put the go command into workspace mode. The go.work file specifies |
| 51 | +a set of local modules that comprise a workspace. When invoked in workspace mode, |
| 52 | +the go command will always select these modules and a consistent set of |
| 53 | +dependencies. |
| 54 | + |
| 55 | +## Parameterized types |
| 56 | + |
| 57 | +.link https://go.dev/doc/tutorial/generics |
| 58 | + |
| 59 | +Generics are the most significant change to Go since the release of Go 1, and |
| 60 | +certainly the largest single language change the Go team have ever made. |
| 61 | + |
| 62 | +## Generics gotchas "return" |
| 63 | + |
| 64 | +.play -edit -numbers 2021-02-04/generic_gotcha_return.go |
| 65 | + |
| 66 | +## Generics gotchas "fmt" 1/2 |
| 67 | + |
| 68 | +.play -edit -numbers 2021-02-04/generic_gotcha_fmt.go |
| 69 | + |
| 70 | +## Generics gotchas "fmt" 2/2 |
| 71 | + |
| 72 | +the go vet error results in: |
| 73 | + |
| 74 | + $ go vet generic_gotcha_fmt.go |
| 75 | + ./generic_gotcha_fmt.go:12:20: fmt.Errorf format %q has arg key of wrong type K |
| 76 | + |
| 77 | +Solution is... |
| 78 | + |
| 79 | +## Generics Interface with type constraints (Problem) |
| 80 | + |
| 81 | +.play -numbers 2021-02-04/generic_gotcha_interface_type_constraint01.go |
| 82 | + |
| 83 | +## Generics Interface with type constraints (Solution) |
| 84 | + |
| 85 | +.play -numbers 2021-02-04/generic_gotcha_interface_type_constraint02.go |
| 86 | + |
| 87 | +## Unusual Generics |
| 88 | + |
| 89 | + // Ptr is a simple function that helps to get literal pointer in one-line. |
| 90 | + func Ptr[T any](v T) *T { return &v } |
| 91 | + |
| 92 | +Invoked as: |
| 93 | + |
| 94 | + pInt := unusual_generics.Ptr(10) |
| 95 | + pStr := unusual_generics.Ptr("test") |
| 96 | + pUint64 := unusual_generics.Ptr[uint64](24) |
| 97 | + fmt.Printf("%[1]T %[1]d\n", *pInt) |
| 98 | + fmt.Printf("%[1]T %[1]s\n", *pStr) |
| 99 | + fmt.Printf("%[1]T %[1]d\n", *pUint64) |
| 100 | + // Output: |
| 101 | + // int 10 |
| 102 | + // string test |
| 103 | + // uint64 24 |
| 104 | + |
| 105 | +Attention: future version Go can call directly `&10` or `&"test"`. |
| 106 | + |
| 107 | +## Generic constraints for checks during compile time |
| 108 | + |
| 109 | +.play -edit -numbers 2021-02-04/generic_avoidnil.go |
| 110 | + |
| 111 | +## Generics: bad patterns |
| 112 | + |
| 113 | +pointing out that: |
| 114 | +``` |
| 115 | +func Something[R io.Reader](r R) error { ... } |
| 116 | +``` |
| 117 | +is a useless use of generics and that is correctly written as: |
| 118 | +``` |
| 119 | +func Something(r io.Reader) error { ... } |
| 120 | +``` |
| 121 | + |
| 122 | +## When to use Generics? |
| 123 | + |
| 124 | +Use them wisely where multiple types occur and not just one. |
0 commit comments