Files
davideisinger.com/content/notes/golang/index.md
2023-07-17 23:20:38 -04:00

7.7 KiB
Raw Blame History

title, date, draft, references
title date draft references
Golang 2023-05-08T09:54:48-04:00 false
title url date file
Why David Yach Loves Go https://cloud.google.com/blog/products/application-modernization/why-david-yach-loves-go 2023-06-13T14:51:05Z cloud-google-com-windxx.txt
title url date file
One process programming notes (with Go and SQLite) https://crawshaw.io/blog/one-process-programming-notes 2023-06-13T14:49:51Z crawshaw-io-k5slfj.txt
title url date file
Go Project Layout https://medium.com/golang-learn/go-project-layout-e5213cdcfaa2 2023-06-13T15:00:02Z medium-com-efpmux.txt
title url date file
Gopher Wrangling. Effective error handling in Go | Stephen's Tech Blog https://stephenn.com/2023/06/gopher-wrangling.-effective-error-handling-in-go/ 2023-06-20T16:25:12Z stephenn-com-kbiijs.txt
title url date file
Effective Go - The Go Programming Language https://go.dev/doc/effective_go 2023-07-12T03:17:03Z go-dev-vfin4x.txt

I find Go really compelling, even though it's not super applicable to my job. When evaluating a new tool, I find I'm weirdly biased to things written in Go.

  • I like that it compiles, and have no desire to install someone else's Python
  • It just seems to hit the right balance of productivity, performance, simplicity, safety
  • The language (and the tech built with the language) just seems built to last

Questions to Answer

Projects I like

Project Ideas

  • Bookmarking app (Pinboard replacement)
  • Note-taking / journaling app
  • StevieBlocks

{{<thumbnail project1 "400x" />}}

Resources

Notes

  • Regular Expressions

    • Compile with regexp.MustCompile (no need to check for error)
    • Strings denoted by backticks don't escape; use these for regular expressions
    • For case-insensitive matching, start the expression with (?i)
  • Unnamed parameters

    Unnamed parameters are perfectly valid. The Parameter declaration from the spec:

    ParameterDecl  = [ IdentifierList ] [ "..." ] Type .
    

    As you can see, the IdentifierList (the identifier name or names) is in square brackets, which means it's optional. Only the Type is required.

    The reason for this is because the names are not really important for someone calling a method or a function. What matters is the types of the parameters and their order. This is detailed in this answer: Getting method parameter names in Golang

  • Named result parameters

    The return or result "parameters" of a Go function can be given names and used as regular variables, just like the incoming parameters. When named, they are initialized to the zero values for their types when the function begins; if the function executes a return statement with no arguments, the current values of the result parameters are used as the returned values.

  • Type Conversion & Assertion

    • Built-in functions for conversion (float64, strconv.Atoi)
    • if v, ok := fnb.(FancyNumber); ok { (v is a FancyNumber if ok is true)
    • switch v := i.(type) { (case per type, v is i cast to that type)
  • Custom error types

    Usually, a struct is used to create a custom error type. By convention, custom error type names should end with Error. Also, it is best to set up the Error() string method with a pointer receiver, see this Stackoverflow comment to learn about the reasoning. Note that this means you need to return a pointer to your custom error otherwise it will not count as error because the non-pointer value does not provide the Error() string method.

  • Maps

    A Go map type looks like this: map[KeyType]ValueType

    To initialize a map, use the built in make function: m = make(map[string]int)

    This statement retrieves the value stored under the key "route" and assigns it to a new variable i: i := m["route"]. If the requested key doesnt exist, we get the value types zero value. In this case the value type is int, so the zero value is 0.

    A two-value assignment tests for the existence of a key: i, ok := m["route"]

  • First-class functions

    • Go supports first-class functions (functions as arguments and return values of other functions)
    • Go has closures (so variables defined inside a function that returns a function get closed over)
    • Go has anonymous functions (func w/o name)
  • Slices

    • Use append to add to a slice; this creates a new slice, so you have to capture the return value (s = append(s, 100))
  • math/rand

    • rand.Intn: random integer between 0 and the specified int
    • rand.Float64: random float between 0.0. and 1.0
    • rand.Shuffle: randomize an array/slice; takes a length and a swap function
  • Stringers

    • Define a String() string method for types so that fmt knows how to stringify them
    • E.g. fmt.Sprintf("%s", yourThing) will call yourThing's String() function
  • Structs

    An anonymous struct is just like a normal struct, but it is defined without a name and therefore cannot be referenced elsewhere in the code.

    Structs in Go are similar to structs in other languages like C. They have typed collections of fields and are used to group data to make it more manageable for us as programmers.

    To create an anonymous struct, just instantiate the instance immediately using a second pair of brackets after declaring the type.

  • Time

    • time.Duration -- time.Second * 1e9 is a duration of one billion seconds
    • 1e9 is the same as 1.0*math.Pow(10, 9)
  • Runes

    • unicode.IsLetter -- test if rune is a letter
    • strings.ContainsRune -- test if string contains letter (strings.ContainsRune(s[i+1:], c))
  • log.Println -- print out anything (like a data structure)