
How I Start: Go - dokuda
http://howistart.org/posts/go/1
======
aaron-lebo
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...](http://www.alexedwards.net/blog/form-validation-and-processing)

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.Schema):
            first_name = validators.ByteString(not_empty=True)
            last_name = validators.ByteString(not_empty=True)
            email = validators.Email(resolve_domain=True)
            username = formencode.All(validators.PlainText(),UniqueUsername())
            password = SecurePassword()
            password_confirm = validators.ByteString()
            chained_validators = [validators.FieldsMatch('password', 'password_confirm')]
    

I thought about how you could design a library like this in Go, but couldn't
think of a way that did not end up using reflection or plain maps, which
defeats the purpose of using static typing.

Is this lack of validation libraries due to the lack of people writing web
apps with the language or is it a problem of expressiveness with the language
itself?

I ran into this issue several times when trying to write a web app in Go. A
lot of the basic infrastructure is there, but the niceties are missing. The
template language, for example, is great but lacks something as basic as
inheritance without kludges. There are other templating languages, but they
don't feel quite there. Got the same feel from the ORMs. Hats off to those who
are doing work in it in spite of this, but it seems there's a way to go before
the language is really usable for web apps.

~~~
murz
> are there any good form validation libraries for Go?

Here are a couple:

[http://www.gorillatoolkit.org/pkg/schema](http://www.gorillatoolkit.org/pkg/schema)

[http://godoc.org/github.com/mccoyst/validate](http://godoc.org/github.com/mccoyst/validate)

There are also some bigger web framework projects that include validation,
Revel is one:
[http://revel.github.io/manual/validation.html](http://revel.github.io/manual/validation.html)

~~~
aaron-lebo
schema is not a validation library, it just turns HTTP forms into Go structs.
Validate uses tags or maps (not a huge issue, granted). The Revel one does
look nice, though. Thanks.

------
eddieroger
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 these out.

~~~
steveklabnik
Hi! I wrote the Ruby one. As far as I know, it's super solid, let me know if
something is weird. :)

I'm going to do a Rust one around the 1.0 timeline too, hopefully.

------
drtse4
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](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 struct.

Nice series of articles.

~~~
sagichmal

        > 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.

~~~
Intermernet
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"'.

~~~
sagichmal
I disagree. (shrug)

------
andrewgleave
I recently wrote a Go weather backend as part of a talk I did on React and
hooked into Forecast's API (which the author mentions as an exercise).

[https://github.com/andrewgleave/react-
weather](https://github.com/andrewgleave/react-weather)

It's very simple, but I'd be interested to hear feedback on making it more
idiomatic Go.

~~~
lfx
Nice, thanks for sharing this. Will play around!

------
kbar13
(with "you" being the author or anyone):

Would you be able to explain why you iterate over the range of providers
instead of iterating over the channel in your concurrency example?

~~~
sagichmal
Range over a channel only terminates when the channel is closed, which that
example doesn't do.

Since the channel is created as buffered, you _could_ do a normal for-loop
over cap(chan), which would be the same as len(providers).

------
ahmett
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.

~~~
sagichmal
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.

~~~
enneff
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.

------
mononcqc
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?

~~~
enneff
You're quite right. The mutliWeatherProvider should probably collect the
errors and only error out if it doesn't get a single good response. Easy to
do.

------
adlawson
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](https://github.com/adlawson/vagrantfiles)

    
    
        curl -O https://raw.githubusercontent.com/adlawson/vagrantfiles/master/go/Vagrantfile
        vagrant up
        ...

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

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

------
daddykotex
Nice post there. Easy to understand and it gives us a solid entry point.

I've been building my own backend for a couple of weeks (yes, very slowly) and
I'm still wondering how to write proper test. Unit testing is a charm and
there is nothing complex there, but mocking my dao package for example is a
little harder.

I'd like to see how you write beautiful unit tests (w/ or w/o mock) in Go.

