diff options
author | Jasper Ras <jaspert.ras@gmail.com> | 2025-05-20 07:19:33 +0200 |
---|---|---|
committer | Jasper Ras <jaspert.ras@gmail.com> | 2025-05-20 07:19:33 +0200 |
commit | a61d928b279c5c508aca3bfc7cb14d810c3d75de (patch) | |
tree | 6df664d4b58f1266e2782252547ed35e3c960b34 /daily | |
parent | d4bd3ad4a869c87fcfa4f83b42555a6c8e1bc746 (diff) |
vault backup: 2025-05-20 07:19:33
Diffstat (limited to 'daily')
-rw-r--r-- | daily/17-May-2025.md | 28 | ||||
-rw-r--r-- | daily/18-May-2025.md | 12 | ||||
-rw-r--r-- | daily/19-May-2025.md | 12 |
3 files changed, 52 insertions, 0 deletions
diff --git a/daily/17-May-2025.md b/daily/17-May-2025.md new file mode 100644 index 0000000..3729110 --- /dev/null +++ b/daily/17-May-2025.md @@ -0,0 +1,28 @@ +# Notes on go +Go compiles into native machine code. +Go handles Unicode natively thus any character can be used in a string. +Divided into packages, where a package is sort of a library, except for main which defines an executable. +Import declarations **must** follow the package declaration. +Function declarations: `func <name> (<param list>) (<result list>)`. No semicolons, unless multiple statements on 1 line. +`gofmt` mandatory. `goimports` (`golang.org/x/tools/cmd/goimports`) manages imports. +Slices are dynamically sized arrays that can be accessed `s[i]` or a subset can be taken `s[i:j]`. Indexing: half-open (starts at 0). `s[i:j]` yields i through j-1. Either can be omitted. + +Convention: comment describing package preceding package declaration. + +Variables initialised to "zero value".7689 +String concatenation via `+` char. +Supports assignment operators, e.g `+=`, `*=` etc +Short-hand variable declaration requires no type, it is inferred of the value `:=` **only allowed in a function** + +For-loop structure: +``` +for initialization; condition; post { + // code +} +``` +Either part can be left away with different semantics. +All parts gone: loop forever. +Only condition is like a while x == true; +`range` keyword to loop over a slice or array with `for index, arg := range slice` + +`_` is the blank identifier, thus above if we don' need the index we can put `_`.
\ No newline at end of file diff --git a/daily/18-May-2025.md b/daily/18-May-2025.md new file mode 100644 index 0000000..2ef64b9 --- /dev/null +++ b/daily/18-May-2025.md @@ -0,0 +1,12 @@ +Some programming problems or programs to make for practicing: +- Echo stdin +- Find duplicate lines on stdin or files passed as arguments. Operating on a stream, or slurp all input and do it at once. + +# Further notes on Go +Order of map iteration is unspecified but random in practice to prevent reliance on ordering. +The Scanner type from the bufio package provides an easy way to read input in lines or words. +Map created with `make` is passed by reference. +Package `io/ioutil` exposes `ReadFile` and other io utility functions and types. +Casting works by `type(var)`, e.g `i := 0; string(i)` +Referencing a multi-component package is done through the last component. E.g `ioutil` for the `io/ioutil` package. +Composite literals are the form of `type{...}` they instantiate composite types, so `[]string{"a"}` is also a composite literal.
\ No newline at end of file diff --git a/daily/19-May-2025.md b/daily/19-May-2025.md new file mode 100644 index 0000000..aa9ab6b --- /dev/null +++ b/daily/19-May-2025.md @@ -0,0 +1,12 @@ +# Another note on Go +`image/color` package features the Color interface defining a Color as just a type that has a method `RGBA() (r, g, b, a uint32)`. It has a struct `RGBA` defining this method. So for example to define green we can say: `color.RGBA{0x00, 0xFF, 0x00, 0xFF}` R G B A respectively. + +Packaging doing stuff with networking are grouped under `net`, e.g `net/http` + +Using `io.Copy` we can copy output from a reader to a writer. This way we can for example copy a HTTP response directly to stdout instead of buffering the entire output first. + +`strings.HasPrefix` exists + +To get a time delta we can use `time.Since` which returns a `time.Time`. We can just call for example `Seconds()` on that to get the elapsed seconds. + +Goroutines run concurrently and communicate over channels, they are started with the `go` keyword. Sending or receiving on a channel block the goroutine.
\ No newline at end of file |