Live data from Hacker News

How I Start: Go

howistart.org

21–30 of 36 posts

Re: How I Start: Go

#21

Earlier quoted context omitted.

>couldn't think of a way that did not end up using reflection The builtin decoders (eg, json) use reflection. You don't lose any of the benefits of static typing as you are still decoding into your statically-typed object. I would separate form parsing from object validation. Use something like this: https://github.com/ajg/form to populate statically-typed go structs from form data, and then validate your objects.

There is a speed hit to reflection, though, right? Also, you do lose the benefit of static typing, for example, the following typo isn't picked up at compile time: type User struct { Name string `form:"nam"` } Tags are rather ugly and don't scale to many options well. What happens when your ORM, form validation library, xml library, and json libraries each need a separate tag? That's a slight exaggeration but you cou…

There is a speed hit to reflection. As for the tag issue: writing your own marshaler isn't actually a huge problem. I have been meaning to write my own json marshaler for a while. I personally find the solution much more paletable than many other ways of marshaling in and out of json.

Re: How I Start: Go

#22
post #9

Don't use println(), use fmt.Println() or other equivalent functions instead. Current implementations provide several built-in functions useful during bootstrapping. These functions are documented for completeness but are not guaranteed to stay in the language. http://golang.org/ref/spec#Bootstrapping And for beginners, it could be interesting to know that decoders also support simple maps in addition to tagged struc…

Don't decode into maps. If you don't want to create a struct, use an anonymous struct.

The only case I can think of making sense is when you deal with JSON that can change shape under your feet (like when dealing with shitty APIs that sometimes returns {}, sometimes []).

Re: How I Start: Go

#23

This article turns into the creation of a small web app, so while not exactly relevant, I'll ask here: are there any good form validation libraries for Go? I ended up using this approach: http://www.alexedwards.net/blog/form-validation-and-processi... This is straightforward but also manual and low-level. In comparison is Python's formencode library which is declarative and high-level: class Registration(formencode.S…

I've attacked this from a few angles myself—previously using reflection and struct tags - `min:"2", max:"50"` - but found it fairly inelegant (although practical). I wrote some good tests around it, that's for sure.

Now I'm a fan of https://github.com/katco-/vala which has a nice API for doing this for you. I've ripped out one of the examples from the README (which I think illustrates it well), but you could also wrap this in a method that you call on the instance of the struct you get back from (i.e.) gorilla/schema:

    func ClearValidation(a, b, c MyType) (err error) {
        err = BeginValidation().Validate(
            IsNotNil(a, "a"),
            IsNotNil(b, "b"),
            IsNotNil(c, "c"),
        ).CheckAndPanic().Validate( // Panic will occur here if a, b, or c are nil.
            HasLen(a.Items, 50, "a.Items"),
            GreaterThan(b.UserCount, 0, "b.UserCount"),
            Equals(c.Name, "Vala", "c.name"),
            Not(Equals(c.FriendlyName, "Foo", "c.FriendlyName")),
         ).Check()

         if err != nil {
             return err
         }

        // ...
    }

Note that for any reasonable amount of validation w/ structs (i.e. not nil, ranging over it automatically, etc) you will need reflection. There are no ifs or buts about that. But the whole 'reflection is slow' mantra is premature optimisation: if you need reflection, you need it. Your template rendering uses reflection, your database connection introduces latency, etc - all of that dwarfs any cost associated with validating a struct or two.

Re: How I Start: Go

#25

This article turns into the creation of a small web app, so while not exactly relevant, I'll ask here: are there any good form validation libraries for Go? I ended up using this approach: http://www.alexedwards.net/blog/form-validation-and-processi... This is straightforward but also manual and low-level. In comparison is Python's formencode library which is declarative and high-level: class Registration(formencode.S…

I've attacked this from a few angles myself—previously using reflection and struct tags - `min:"2", max:"50"` - but found it fairly inelegant (although practical). I wrote some good tests around it, that's for sure. Now I'm a fan of https://github.com/katco-/vala which has a nice API for doing this for you. I've ripped out one of the examples from the README (which I think illustrates it well), but you could also wra…

Thanks for your response. It is good to hear from someone who has wrestled with the issue.

Re: How I Start: Go

#26
post #14

It's actually annoying Go is shown as a web language in many tutorials. It's simply not. If you use Go for systems programming and other stuff like creating daemons, services, you will fall in love with it more and will not go back to any other language. That's why I believe those Go start tutorials should not be showing the net/http side of things.

Go definitely shines as a server language, so I think some kind of server is indeed the best way to illustrate Go's capabilities. It's true that net/http is kind of... boring, and maybe not the best choice for a beginner tutorial due to some of its quirks, but it has the benefit of being a well-understood reference point.

In reality, this tutorial is about making the API calls and decoding their JSON responses, and then introducing some concurrency to do multiple calls in parallel. The HTTP portion of this tutorial functions as some familiar ground for newbies, a nice way to get a grasp of the Go style in a context that many people understand.

Re: How I Start: Go

#27

I like the idea of this series, because I think it's more useful than Hello World type examples. There's an implicit trust that whoever is writing the article knows what they're doing, which could be a problem for someone new to a community. I skimmed over the Ruby one (I know Ruby better than Go), and it seemed pretty solid, and was written by a Rails core contributor, so it should be. Hopefully they'll get more of…

The whole point of this series is to pick people who are knowledgeable in their respective language. Jose wrote Elixir and Fred is a known personality in the Erlang community.

It is so much more powerful when it is people with knowledge who writes these!

Re: How I Start: Go

#28
post #9

Don't use println(), use fmt.Println() or other equivalent functions instead. Current implementations provide several built-in functions useful during bootstrapping. These functions are documented for completeness but are not guaranteed to stay in the language. http://golang.org/ref/spec#Bootstrapping And for beginners, it could be interesting to know that decoders also support simple maps in addition to tagged struc…

> Don't use println() println() is nice because it doesn't require an import. For "the smallest possible program" I think it's a good match. > decoders also support simple maps in addition to tagged > structs I think it's a mistake to teach Go programmers to decode into map[string]interface{}. It's pretty rare that you actually want to do that.

Yes, but as stated above "it is not guaranteed to stay in the language". The functions from "fmt" are guaranteed to stay in the language.

May as well start learning the language with best practices, even if it does involve an extra line to 'import "fmt"'.

Re: How I Start: Go

#29
post #11
post #5

If you'd prefer to play with golang on linux, I've got quick a Vagrantfile ready to go at https://github.com/adlawson/vagrantfiles curl -O https://raw.githubusercontent.com/adlawson/vagrantfiles/master/go/Vagrantfile vagrant up ...

Why do you assume we're all using a Mac?

Vagrant is available for Mac, Linux and Windows (https://www.vagrantup.com/downloads.html). I'm not sure what other dependency the link above would have.

Re: How I Start: Go

#30
Am I right in my understanding that the end of the last code snippet shows that the app will error out for the user every time any single one of the many APIs used is unavailable, rather than making an average of the remaining services (1 of them)?

Isn't this approach making the service's downtime be the sum of the downtime (minus overlap) from all the APIs being called?

Post reply on HN