God Speed You! Black Emperor is playing in the kitchen, the sound of cursed air planes can be heard, children shouting, cars driving. I’ve just had some self-made bread with cheese and now I’m drinking tea.
Hex Populate remains an interesting project, mainly for two reasons:
1. It’s my first and only real Go project.
2. It’s an online application without a web user interface.
That’s right. It remains strictly available via SSH and SCP. You connect to it via SSH, and use it. When you save it, the files are only kept for as long as your connection remains active, so you need to copy the files to your computer using SCP before quitting.
Image 1 for 2022-09-08 Writing Go code
I’m currently learning about writing unit tests. 🙂
I’m also starting to get the hang of how to use the `go doc` command. Very nice.
Things that I like, and that remind me of Perl:
I think I could get used to writing the code.
There’s white-space stuff that ends up being easy to understand. Basically, you can always replace a semicolon to end a statement with a newline, and you can add extra newlines if the preceding expression is sure to require more tokens, e.g. ending the line with an operator that requires another operand. This works surprisingly well. It’s better than C and Perl and not as bad as Python.
Here’s a thing I felt was annoying: initialising multidimensional arrays.
func initial_flow() [][]direction { flow := make([][]direction, max_width) for x := 0; x < max_width; x++ { flow[x] = make([]direction, max_height) for y := 0; y < max_height; y++ { flow[x][y] = none } } return flow }
Here’s a think I like: `iota` automatically starts an enumeration. Types are simple to use and casting works.
type direction byte const ( north direction = iota northeast southeast south southwest northwest none ) func neighbours(m model, x, y int) map[direction]position { neighbours := make(map[direction]position) for d, p := range m.neighbour[x][y] { if valid(m, p.x, p.y) { neighbours[direction(d)] = p } } return neighbours }
There’s probably lots of style issues with my code. Remember the case shorthands? camelCase, PascalCase, kebab-case, snake_case. I like kebab-case best because it works in Lisp, but when I can’t use it, I use snake_case, like in Perl. I think the Go people use camelCase for private names, oops. Since I don’t write a library, I haven’t had to use PascalCase – that’s what they use for public names. Oh well.
If you check it out and notice issues, let me know. As I said, this is my first Go project.
#Programming #Go #Hex Populate
(Please contact me if you want to remove your comment.)
⁂
Strangely enough, when assigning arrays these don’t get copied, even though blog posts say that this is how it works. Is this SEO destroying the utility of looking up code examples online? (Edit: this is a slice, not an array → see below for more.)
package main import "fmt" func main() { a := []int{1, 2, 3} b := a b[1] = 5 fmt.Println(a, b) // [1 5 3] [1 5 3] }
In my particular case, I sometimes need a list of positions to go through, sorted by altitude. I have an array of positions but the index is meaningful, it’s the id. So I want a sorted copy of this array of positions and for the longest time I kept corrupting my original array of positions. Oh well.
So now I’m copying the array like an animal, by hand.
package main import "fmt" func main() { a := []int{1, 2, 3} b := make([]int, len(a)) for i, v := range a { b[i] = v } b[1] = 5 fmt.Println(a, b) }
– Alex 2022-09-08 18:07 UTC
---
This should work:
copy(b, a)
– r 2022-09-09 05:06 UTC
---
Thanks!
I’m still grappling with the issue because I keep reading sentences like the following:
A copy of the array will be automatically created when
An array variable is assigned to another array variable.
The following therefore works:
package main import "fmt" type model struct{ p [3]int } func main() { m := model{} m.p[0] = 4 m.p[1] = 4 m.p[2] = 4 q := m.p q[1] = 5 fmt.Println(m.p, q) // [4 4 4] [4 5 4] }
But in my case, I’m using slices, not arrays, and now this “automatic copying” no longer works:
package main import "fmt" type model struct{ p []int } func main() { m := model{} m.p = append(m.p, 4) m.p = append(m.p, 4) m.p = append(m.p, 4) q := m.p q[1] = 5 fmt.Println(m.p, q) // [4 5 4] [4 5 4] }
I’ll now go and add the copy command in one or two places of my project. 😀
– Alex 2022-09-09 06:44 UTC