diff --git a/src/blog/posts/2024/5/5.11tydata.json b/src/blog/posts/2024/5/5.11tydata.json new file mode 100644 index 0000000..691ea07 --- /dev/null +++ b/src/blog/posts/2024/5/5.11tydata.json @@ -0,0 +1,3 @@ +{ + "tags": ["WeblogPoMo2024"] +} \ No newline at end of file diff --git a/src/blog/posts/2024/5/learning-go-day-1.md b/src/blog/posts/2024/5/learning-go-day-1.md index 3fb74a0..da1d9ce 100644 --- a/src/blog/posts/2024/5/learning-go-day-1.md +++ b/src/blog/posts/2024/5/learning-go-day-1.md @@ -2,10 +2,8 @@ title: "Learning Go: Day One" date: 2024-05-01T08:00:00.0Z tags: - - tech - learning - go - - WeblogPoMo2024 excerpt: "I've wanted to try learning Go for a while now, but have never got round to it. So I'm going to (attempt) to learn a little bit about it each day, and blog about it, with a view to building something with it by the end of the month." --- diff --git a/src/blog/posts/2024/5/learning-go-day-2.md b/src/blog/posts/2024/5/learning-go-day-2.md index 091a421..a55c87c 100644 --- a/src/blog/posts/2024/5/learning-go-day-2.md +++ b/src/blog/posts/2024/5/learning-go-day-2.md @@ -2,10 +2,8 @@ title: "Learning Go: Day Two" date: 2024-05-02T08:00:00.0Z tags: - - tech - learning - go - - WeblogPoMo2024 excerpt: "In part two, I take a look at how to organise code into packages" --- diff --git a/src/blog/posts/2024/5/learning-go-day-3.md b/src/blog/posts/2024/5/learning-go-day-3.md new file mode 100644 index 0000000..78d74c2 --- /dev/null +++ b/src/blog/posts/2024/5/learning-go-day-3.md @@ -0,0 +1,119 @@ +--- +title: "Learning Go: Day Three" +date: 2024-05-03T08:00:00.0Z +tags: + - learning + - go +excerpt: "For my third day of learning Go, I'm going to take a look at some control structures" +--- + +Over the last two days I've learned how to [setup and create a Go project](/post/learning-go-day-1/), and then [how to organise code into packages](/post/learning-go-day-2). I realise I've skipped a crucial step there, though, which is learning how to deal with control structures. + +By control structures, I'm referring to things like `for-` loops, `if` statements, etc. You know, the things that make the software do things beyond just multiplying 2 by 5. + +## For-loops + +The old trusty workhorse of any programming language. These seem pretty straightforward in Go, similar to most other languages: + +```go +// maths.go + +// Calculate val to the n'th power +func Pow(val, n int) int { + for i:= 1; i < n; i++ { + val *= val + } + + return val +} + +// main.go + +func main() { + four_squared := maths.Pow(4, 2) + fmt.Printf("4^2 = %d\n", four_squared) +} +``` + +Cool, this worked. I have successfully reinvented a small wheel[^1]. + +## Do-While + +The _other_ trusty workhorse! Most languages have some form of this construct that says something like: + +``` +while statement is true: + do something +``` + +But not Go, apparently! Or at least, not explicitly. In for-loops, the first and last arguments - defining a variable and performing an operation on it - are totally optional, so a while loop is just a for-loop[^2]: + +```go +// maths.go + +func Mod(a, b int) int { + remainder := a + for remainder >= b { + remainder -= b + } + return remainder +} +``` + +## If-else-then + +The third trusty workhorseman of the Gopocalypse[^3]. Basic if-statements are pretty straightforward: + +```go +// maths.go + +func Divide(a, b int) int { + if a == 0 { + return 0 + } + + if b == 0 { + return 0 + } + + return a / b +} +``` + +Which could be cleaned up to use else-if: + +```go +// maths.go + +func Divide(a, b int) int { + if a == 0 { + return 0 + } else if b == 0 { + return 0 + } +} +``` + +You can also add variable declarations to if-statements, which are then only accessible inside the blocks: + +```go +// maths.go + +func Min(a, b int) int { + if v := a - b; v > 0 { + return b + } else { + return a + } +} +``` + +Okay, so that's covered most of the control structures off the top of my head. On my to-do list for the next couple of days are: working with arrays, and testing. + +[^1]: This particular wheel is a lot slower than the original wheel, but hey, I'm learning +[^2]: Yes, yes, I know that's actually the case in many languages and `while` is just syntactic sugar +[^3]: I'm sorry. + + + +