Hacker News new | past | comments | ask | show | jobs | submit login
Learn Go: Hand-crafted Go exercises and examples (github.com/inancgumus)
222 points by inancgumus on May 16, 2020 | hide | past | favorite | 66 comments



Observation: I see that you do 26 exercises before doing anything with pointers.

I often wonder if one of the reasons Go is more "simple" or approachable to some is because you can, to a large extent, ignore pointers and interfaces and "just write that weird little * or & in some places" and get away with it. Whereas, I believe in other languages, this is much less possible (e.g. in Java or Rust you need to learn about less "just logic" traits of the language earlier on (class inheritance, generics or Options, borrow checking, etc.)

I'm not saying the above is a good thing, but I often wonder if there aren't ways to make this more of an incremental learning curve in other languages in a similar way that you can largely ignore pointers in Go for a long time and be productive without understanding them. What would a similar incremental learning curve for generics be?


I think (and this is, somehow, related to pointers) the most complicated thing in go for a beginner (and even for someone who can program but only knows higher level languages, java included) is slices.

Slices are hard to grasp, you can modify them within a function (without ever touching that weird little * and his & cousin) and they are still modified when you exit the function, but you can't do that with other values (all of them need the star operator if you want to modify them).

Plus, appending a value to the end of a slice is very cumbersome and unusual with something like

    arr = append(arr, val)
Oh, and do you remember when i said you could modify slices within functions and they are still modified in the end? This is not true with append. If you append to a slice, the new values won't be appended when you return from the function.

This thing doesn't really make sense unless you've been a C programmer in fact, and understand the concept of fixed-size arrays and reallocating memory to grow them. Otherwise, even if you can work around that weird part, it will always seem a little magical and tricky.


100% agree - slices are a very leaky abstraction in Go


Your can't really do anything real without pointers in Go though because you won't be able to mutate states out of a function.


That's definitely true, but I've often seen beginners just say:

    type Foo struct { ... }
    func (f *Foo) Bar() { ... }
And go "Aha, that's an object and a method, I get it!" but when asked whether they should be using a pointer there or not, have no idea what that even entails.

In other words, it's possible to neglect the details of pointers easily and have things generally work


William Kennedy has an excellent post on this https://www.ardanlabs.com/blog/2017/06/design-philosophy-on-... As someone who has written (and read) a decent amount of Go the value vs pointer semantics of the function informs me of how the creator intends for the type to be used. Seeing pointer receivers informs me that there is an expectation of modifying the state while value pointers (which are thankfully more common) return a copy of the data.


> Tom has also mentioned that a box of copy paper can hold 100k lines of code.

that comes out to 20 loc per 8.5x11 piece of printer paper.

I feel like that's nearly an order or magnitude too low.


Remember ByVal and ByRef from VB? (I hope you don't, it was the dark ages) but those to terms I've found are very useful for getting folk introduced to pointer - and also why.


That is really interesting to me! I never used VB, is the only difference here that it is "named" ByVal and ByRef that you think made it easier for people to understand than "pointers"?


Well, I use those terms (ByVal/Ref) and then say, one is a copy (byVal) and the other is a reference (pointer to you and me) to the thing.

So, pass small things ByVal (int, float), it's on the Stack.

Pass big things (Object) ByRef cause they're in the Heap.

Then I start saying pointer more than ByRef and the link is made.

Then on to ByRef/Pointer to how that then manipulate the shared data.

Once that basic is done, we refine/clarify around what Pointer really is, and also it's syntax.


I'm not sure I've seen this explained so well in so few words. Thanks!


I learned Rust & Go pretty much at the same time, and it actually works quite well because most of the paradigms are actually the same (structs & function implementation on top of them, error as return value). Rust is clearer about the point you mention[1], while Go introduction spend more time explaining things like threads and channels (which I wasn't familiar with), and finally Rust help you to use them properly, thanks to the Send & Sync trait, which make you realize when you're doing racy things.

[1]: https://doc.rust-lang.org/stable/book/ch05-03-method-syntax....


Yeah, Go focusing on threads and channels I agree is generally a painful new concept for beginners to learn. I've also seen beginners often "over-correct" and start using channels and goroutines for things they really shouldn't.

How was your learning experience with Rust's borrow checking and pointers? Was it introduced to you early on, or later on?


I learned Rust through the book (first edition), which at this time was the only available material, and it was explained right at the beginning (at least borrowing, ownership and mutable borrows).

I like when things are explained upfront, and actually my biggest issue when learning Rust came from not having the smart pointers (like RefCell and Rc, which relax the ownership constraints) explained at the same times as regular ones.


This is me right now. I get the basics of Go, but I don't understand when i'm suppose to have something be a pointer, and when i'm suppose to use * or & . Anyone who has a good explanation in a EL5 way, would help me a lot!!


Imagine we're working together and I have some text document I want you to work on and update. If I send you the text by email, I send a copy of it (the original text is still on my computer). If you modify that copy and keep it for you, I won't ever know what you did. I just used a function

    func (c Coworker) SendForUpdates(d Document) {
        ...
    }
That wouldn't make sense. You worked hard and I don't even know what you did. So, what I would expect you to do is, once you made updates on the copy, to send me back that copy by email. That would be akin to

    func (c Coworker) SendForUpdates(d Document) Document {
        ...
        return d
    }
I sent you a copy, and you returned another updated copy. That is "pass-by-value", the default, no-pointer style.

Now, let's say I think those emails back and forth and boring. Rather than sending you a copy of the text each time, I could rather use Google Docs, and send you the link to that document. Its URL, rather than a copy of its content. Now, you can just go to that URL and do the updates on the document. You don't have to send me back the document: you're working on it, not on a copy of it! Well, that URL is a reference to the document rather than the document itself, or, if you prefer, a pointer to it. So, now, the function would be

    func (c Coworker) SendForUpdates(d *Document) {
        ...
    }
And we're done, no more back-and-forth dance now! That is "pass-by-reference".

You don't only use "pass-by-reference" just to be able to check updates on the document sent, by the way. If I want to send you some text just for your information and I don't expect any kind of update, I'll use pass-by-value (the very first function). But what if I want to send you a 3 GB video? I can't send that through e-mail! Sending a copy would be totally inefficient. Once again, I'll send you a pointer, an URL to download the video:

    func (c Coworker) InformText(d Document) // d is small: pass-by-value

    func (c Coworker) InformBigVideo(v *Video) // videos are huge: pass-by-reference
Why not use pointers everywhere by default, they seem easier, right? That's basically what java and python do. Well, they can be tricky too. I gave you the URL to the link and you could work on it. Once you're done, I don't want you to modify the document anymore. I want to send it to our boss. But, how could I know you didn't keep the URL somewhere in your bookmarks? How do I know, of all the coworkers I sent the URL to, one of them doesn't keep on updating that document even when I don't want to anymore? With copies, I'm safe, do whatever the hell you want with your copy, I don't care anymore. But a reference to the original document? That can be dangerous.


>That's basically what java and python do. Well, they can be tricky too.

But,you can easily make the classes/collections immutable to avoid the issues you mentioned. I think in java records and many collections are immutable by default. Immutability is fundamental to functional style programming.


This is a phenomenal explanation. Thank you - I've saved this for next time I need to explain to a junior.


You want a function that accepts a pointer when you want to modify an outside value within a function without returning the value. In that case, the func accepts a pointer (like in `func(p int)`) and you pass it either a pointer (a var declared as an {some_type}) or the address of a variable (with &variable).


Challenge accepted https://play.golang.org/p/iTwreChPZH8

ᕕ(¬ ͜ ¬)ᕗ


Slices are (fat) pointers though ;)


Yes you can, lots of functional languages work without mutation. I'd go as far as to say mutating should be the exception, not the default, in more traditional languages.


Go misses almost all tools a functional programming language offers (no iterator or combinators of any kind) and even if you really wanted and decided the verbosity wasn't a problem for you, I'm pretty sure the compiler doesn't optimize it well (it does a lot less optimisations than LLVM for instance).


But those things don't make a language functional. It's like saying there are no lambda statements as there is no lambda syntax, but syntax != feature.

I mean I see your point, and if I really wanted to be fully functional I'd use a language _with_ those things like Haskell. But not being mutable is easy in Go too.


So true. When I first stated writing Go I would always try and * or &


Sure they're hand crafted but are they artisinal and free range and carbon neutral?


They were baked in a wood fire oven, with the classic Napoleon style.


This looks pretty cool. When I was learning Go a long time ago, I used a site called Gophercises and found it really helped me understand the language better.


I have not yet been able to find a simple "Hello World" example of starting a new project with Go modules instead of $GOPATH.


Hey there I found modules to be confusing coming from GOPATH too, but they're straight forward, super barebones example:

    #!/bin/bash

    cd $HOME

    # Create project directory
    mkdir hellogo

    cd hellogo

    # Init project modfile
    go mod init example.com/hellogo

    # Create the main source file
    # Uses bash's heredoc
    cat << EOF > ./main.go
    package main
    import "fmt"
    func main() {
        fmt.Print("Hello world")
    }
    EOF

    # Build it
    go build

    # Run it
    ./hellogo


You can also skip building, and run directly using `go run`!


Actually, the official Go Blog has a decent "Hello World" example[0] for go modules - I've used this before, had to double check. [0] https://blog.golang.org/using-go-modules


go mod init github.com/your/project

Then, go get -v will download your dependencies to GOPATH, and (re)generate go.mod, go.sum. For info on how to update dependencies, read `go help get`.

When using modules (presence of a valid go.mod file), GOPATH won't be used for resolving imports. So you don't have to actually work in GOPATH anymore, dependencies are just stored there.


I've compiled a few interesting quizzes about Go's slices: https://medium.com/@gotzmann/so-you-think-you-know-go-c5164b...


Why snaps? Is apt-get not good for you already? What's wrong with it?


If you're interested in interactive language learning exercises, then try codewars(https://www.codewars.com)


I can never tell whether these titles are referring to the programming language or the board game.


Neither can I but it’s not much of a problem because I’m interested in both!


Good one! BTW, it is 2am here :-)


I was disappointed to see it was golang. Is there a site like this for the game?


http://playgo.to/iwtg/en/ for basic rules, http://www.goproblems.com/ for building skill, https://openstudyroom.org/ for learning with others


This doesn't ask the most important question: why would I learn Go at all?


In case the question isn't facetious:

Go's main advantage, IMO, is that is both simple and opinionated to the extreme. It's a language that makes a lot of choices for you (from "no while loop" through "no exceptions" to where the brackets go formatting the file). Those choices might be the best choice or not, but the point is that they're already made and they're enforced.

The result is that in a big company, all projects made in go are very similar to each other in a lot of ways (much more than the average language) and so it is as easy as possible to have people contributing in each other's projects or moving between teams. There's no arcane syntax, no discussion about linters, nothing like that.

It's basically an amazing language to make programmers interchangeable. Whether that's good or not is a different discussion, but at the very least it looks that it's gonna make it very attractive for employers and so be a highly demanded skill in the near future.


This is very weak argument. Once these restrictions are embedded in language, it cannot be removed by anyone. On the other hand, same thing can be accomplished by using company-wise style enforcers. If you think that is harder, I'd say forcing every project in big company to use same language is even more harder.

My main quip with Go is that its rather more of the same but more poorly done in the name of minimalism. Extreme minimalism is as bad as extreme richness. The entire art of language design is achieving just the right balance for a specific audience and set of goals.


But "company wide linters" do carry over to other companies and projects. For Go its already set up, you dont have to think about it. Otherwise youll have a week (or more) discussion for every single quirk in the language and how one would format that and try to apply this to a group of people.

I'd call them more like working standards. Imagine having to discuss how to make a brick wall everytime you need to make one. Things like having a masonry cord (which can equal to a "gutter", line length restriction) and the mixture of the cement, are critical for execution in a team of more than 1 person, and not following standards will make a hacked up job.


s/do carry/do not carry/


> On the other hand, same thing can be accomplished by using company-wise style enforcers

But you can't do that with open-source libraries. When I use an open source library, I can audit its code very easily, and this is not always true with other languages. The more complex the language, the harder it is to audit it. There are many C++ open source libraries (hello Boost) I can't understand because they don't use the dialects I'm used to.


To answer the question: what would a language look like if you put it's designers in a time capsule for 20+ years and had them make a language that was uninformed by all the language advances and learnings they'd missed out on.


This doesn’t answer the question at all. Also it’s very disingenuous. The designers of Go were very much aware of the the last 20 years of language development. They decided on a subset that catered to software engineering between teams of people spanning a length of time.

It purposefully leaves out lots of features and cutting edge design philosophies because many Of those make things difficult when sharing code between multiple developers Spread over a long length of time.

Go’s philosophy has always been close to KISS. Don’t provide all these cutting edge tricks and tips because you can accomplish the same thing in a far clearer and maintainable way by doing it simpler.


This is the story I keep being told, but it doesn't seem to match with the reality of a language. Two example off the top of my head:

Loop variables are captured by "reference", not by value, so it's very easy to create bugs where you capture accidentally capture the wrong thing and don't have the value you'd expect.

Nulls, the billion dollar mistake. Most languages are quickly moving away from nulls (and pointers for that matter), or creating constructs that make them much safer (such as what typescript is doing). Instead go doubles down on both of these. In the last decade of programming in kernel level c, python, ruby, c++, java, typescript, scala I've never worked with code that crashes so much and is as buggy as go.

Both of these problems could have been addressed fairly easily without bloating the language. Google has people on the c++ committee; it very much feels like the creators of go had too much hubris to walk down the hall or across the campus and kindly ask a good language designer to shoot holes in their design.

Another example is "iota". How does that make code sharing easy at scale? Any time I see iota, I have to start manually counting lines, and remember the rules for if blank lines or comments bump the counter, and it completely circumvents the ability to quickly grep for a value seen in a log message. It is completely antithetical to teams of people and spans of time and whatnot. It seems more like a team of three people who randomly had ideas and ran with them without thinking the consequences through very well, or consulting the wisdom of others.


> Nulls, the billion dollar mistake.

That "billion dollar mistakes" is an excellent marketing expression (nobody wants to make billion-dollar mistakes! We should avoid that null they talk about! It looks so expensive!) but I don't know how actually true it is. Do we have any kind of scientific paper that proves languages without null lead to way less expensive software than languages with null?

I'm not talking about memory unsafe languages like C or C++, but situations where, in a memory-safe language, a null pointer exception in production happened to cost a shitload of money.

I'm pretty sure I never had a nil dereferencing in production with my go code. Invalid array access, off-by-one-errors, yeah, sure, way too many, but very few languages can prevent them at compile time. But nil dereferencing? I can't remember that.


Kotlin is one example where it's difficult to make that mistake. Kotlin does provide nullable type and everything can check at compile time and it so good. So if write code in pure kotlin (java interop has null issues) you can avoid null pointer errors. It's really good.


I totally agree it's great and tend to enjoy languages that don't provide the user with unchecked nulls. It's a cool tool, but I don't think it's worth a billion dollars. If I had to choose, I'd rather take non-mutability by default rather than compile-time enforced checking of null pointers.


I'd read some post where Go designers boasted about how they simplified compiler code by cutting down a language feature. It seemed to me they were obsessed about keeping compiler simple which resulted in half-assed language that was then re-casted as achievement in minimalism. I've felt that Go is optimized for compiler designers as opposed to actual developers.


As a Go programmer myself, the syntax of Go is simple yet effective. You are in control of what should be done, and Go will make sure all edgecases are covered in _defined_ behaviour like a managed language would. In C, reading out of bounds is legal (except when the address is not accesdible). In Go, you will get a panic (that you can still catch, but its pointing out an invalid program state).


But how noteworthy is that? I'm sure programmers coming from C will appreciate it, but well-definedness is kind of… the bare minimum you'd expect from a modern language, especially a fairly high-level one like Go.


I dunno, I feel like Go was created to commoditize the work of programmers. Make everyone write the same stupid and repetitive code, over and over again. No one will ever be able to fuck it up, and no one will ever do anything interesting with it. You can onboard new hires who don't know the language in literally an hour. When reading the code, everyone will understand what each line does but there will be so many lines in the codebase that most people won't actually have time to understand the entire project (which is probably how the managers prefer it, actually). The performance will be pretty okay, since the language isn't high level enough to not be aware of the performance of the code you're writing. Great for companies with a gazillion coders. Like Google.

Programming in Go is like trying to tie your shoes with one hand. You can do it, and its okay (I guess), but you can always see how things could be so much easier.

Maybe this is a bit much, but I find Go offensive: designed for the maximum amount of typing and least amount of thinking. Even Java looks like a powerful language in comparison.


I think certain decisions, like ditching inheritance, were very much informed by the last 20 years of language 'advances'.

They certainly got some things wrong (null for example, and errors aren't great, generic collections would be nice), but it's quite interesting just how much they left out while making a very usable (IMO) language.

I don't want advances and progress in a programming language, I want it to get out of the way and let me think.


Good point!


Painting in very broad strokes here: If you are coming from C, because you're willing to trade a little bit of speed and footprint to no longer deal with malloc and free. If you're coming from a higher-level, dynamic language, because you're willing to give up some dynamism in order to gain speed and possibly concurrency.


Same reason you learn any language. If you have zero interest in learning Go, why did you click or comment on this link? If you’re genuinely interested in what Go programming is like, the Go website has a Tour that is concise and well made.


Why should one live at all?


I was wondering the same thing, and it's sad to see this question downvoted. HN discussion quality is turning into Reddit.


It’s downvoted because it’s being facetious. If you actually want to learn about what Go programming is like or used for, go to the Go website and try the tour.


This is great, but I think it should be written in Rust.

It will then run way faster and be completely bug free.


Their is nothing like a bug free code as a result of the programming language used . RUST programmers are like the new version of those Haskell programmers that spams every functional programming thread with a message that tldr: "write everything in Haskell or die "




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: