61 comments

[ 3.3 ms ] story [ 139 ms ] thread
I am the author of this book, I'd love to get feedback on the book. I haven't updated much in the last few months. But I am always looking to improve the book for newcomers.
Don't you think that teaching how to use it with mysql or postegres would be more useful than sqlite?
That seems a bit overkill. With telling a user how to use mysql/postgres/&c., there will also be questions about setting up these systems, so it seems that "just use sqlite" is a valid path if you're just wanting to go over basic usage.
I would second this. One big issue I faced with Go is the lack of fb connectors. Had to scrap one project because of no Teradata connectivity.
The code is typically identical/reusable, it's all using the same stdlib database package. The only two lines that would change would be the import (import a mysql or postgres driver instead of sqlite) and the database handle - `db, err := sql.Open("sqlite3", "./newtask.db")` would become something like `db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")`. One of the many bright spots of Go.
There are a few more use cases where it changes with postgres iirc and a few are not obvious.

For example the Result type's LastInsertId() method doesnt work in a few cases for postgres that it does for others.

Well, as others have pointed out, sqlite is simple and easy to use, that way we can work on the core functions of the app rather than having to worry about the database.

Also, as far as Go language is concerned, we do not really have to worry about the underlying database, the code, as others have rightly pointed out, is totally reusable

I would also think that BoltDB would be better than SQLite
Huh? Why? Sqlite is closer to postgres/MySQL is you're looking for a reference base and actually sqlite is a pretty awesome database. Bolt DB is much more niche.
Just looking at the template bit... why do you use text/template and not html/template?

I get that you wrote the templates, you trust them, you don't want the HTML escaping, etc... but is that true of the content you display?

Also missing (in general, from Go instructionals on web apps that dare to stray into HTML) is internationalisation and localisation.

Usually that's a goimports gotcha. Definitely want to be using html/template though.
> internationalisation and localisation

These will be the next topics to be added :-)

Also, I use visual studio code, so haven't really written import statements on my own! I'll use html/template, because no matter who wrote the templates, escaping them is a good idea

FWIW, the reason you're interested in escaping HTML templates is not usually the templates themselves but the user content that gets injected into them.
I've been using this, found it through Google awhile back. I didn't want a framework and this helps. I had to search more for Redis and PG and Solr but the front-end stuff here (html,css,js) was helpful

+1 to switch html/template

I am glad you are using the book :-)

I think we can add a chapter on Redis then!

That was a goimport gotcha, I use visual studio code, so have never really written imports.

> Till now we never used any third party library or a framework in this book, this is for the first time that we are doing so, as per the arrogant people on HN we better use libraries for handling sessions, since security is the #1 aspect of any web application,

Stackoverflow is worse in some ways. I'm not sure you (or anyone or Harvard) can ever please HN, so unless that is the goal of your book, why bother?

I am sorry, I didn't understand your comment.
Why do you mention HN at all? Doesn't seem relevant. It's just a place where people come to disagree.
Well I was angry, I see that there is no point of that line, I'll remove it today.
I much prefer to serve Go applications through an NGINX reverse proxy.

It deals with a lot of stuff for you (static assets, SSL, logging, etc.) and lets you focus on developing your application rather than re-inventing the wheel.

Would be a good idea to add in a section on configuring that type of setup.

I am sorry for sounding naive, but I have never used a reverse proxy before, I'll work on it though and add a chapter as soon as possible. It'd be great if you can provide me some pointers :)
Not many pointers needed, a simple reverse proxy to your local Go app will provide all of the good, mature stuff that NGINX provides, most noteworthy good, simple plug-and-play logging:

        location / {
                proxy_pass http://127.0.0.1:8088;
        }
This of course proxies everything to your Go app; you'd probably want to let NGINX handle static assets as well.
Just think of it as a load balancer. Just something that sits in front of your application and gives you the opportunity to determine how to handle things before they get to your application.

Usually things like serving images, css, automatically compressing them, automatically compressing other traffic, domain redirects, ssl, etc. Helps keep your application focused although I realize it can be tempting to just do all that inside of go since it's somewhat good at it.

(comment deleted)
Another argument in favor of running NGINX in front of a Go webserver is that Golang's web server doesn't do any compression by default. It's possible to add in Go via a middleware but it's very simple to configure in NGINX. Also, NGINX's C implementation of compression should be faster than a pure Go implementation.
With Caddy[0] around, the only thing that keeps me tied to nginx is its performance and tried-and-tested-ness.

Waiting to see someone do some detailed benchmarks on Caddy before I commit to it for production stuff, because I'm too busy and too lazy to do them myself and I have a while left on my paid TLS certs.

I mention Caddy because it includes LetsEncrypt and HTTP/2 out of the box — and is written in Go.

[0] https://caddyserver.com

Caddy also just received one of the Mozilla OSS grants/awards.
"the only thing" keeping you with nginx is virtually the most important thing that I want out of my (reverse proxying) web server.
Haproxy is great for this as well. I use it just for SSL offloading and compression, and leave the static files to Go.
Like you describe in the book, I had written a web-app in Go in which I was doing all the SQL handling manually, I found that it became very difficult to maintain this over time as I added new columns and more tables. Just the other day I switched to using GORM (github.com/jinzhu/gorm) for my database drivers and so far it's been a life-saver. Since this is a book about not using frameworks, GORM might be overkill for what you want to do, but I thought it was worth mentioning.
I used to think that using raw SQL is better than using ORMs because I have had a bad experience using django1's OEM. I will look into the ORM now. Thanks for the advice!
Side comment, and I'm sure it has been said before, but GitBook is beautiful. It could replace textbooks, cheap $40 tablets can render HTML just fine.

I'm going to read this because I've never done any work with Go, but I feel like I should learn it and websites/apps are in my domain.

Thanks for the work

Yes git book is amazing. It can totally replace books, it being open source is just amazing.

Thank you for the appreciation!

I feel old-fashioned for still preferring books printed on physical media...
I love printed books, have built an entire library at my home. Nor a single ebook. Don't own a kindle.

But for programming books i prefer online

Sorry for not adding much to the conversation, but I've never seen git used for a whole book before. It's such a cool idea.
Sorry for not adding much to the conversation, but I've never seen git used for a whole book before. It's such a cool idea.
Well mine isn't the first book created with git, my book is actually inspired from astaxie's book on Go, that too is on github
I applaud the approach and the effort, but there are some basic things in this book that are just not correct, or are difficult to read.

One random page that I jumped to: https://thewhitetulip.gitbooks.io/webapp-with-golang-anti-te...

> Go doesn't allow us to have functions as a part of structs

I don't know what this means. Functions can be fields in structs.

> These methods can only be called by an instance of the struct.

That's not true. Methods are just regular functions, with some syntactic sugar to make them look like methods from other programming languages. They are not dispatched on the receiver value, but rather based on the type.

https://play.golang.org/p/ER2r58_BRz

It's "nice effort" in similar sense that 2 year old girl's painting is "nice painting". Almost every sentence (I've read) has unintentional message between lines saying that the author is very young and an amateurish programmer.

Who knows, maybe this is the best way to learn anything!

Just write down what you know as gitbook and let people send you corrections - just the things you need to know, brilliant! :)

This comment violates the Show HN guidelines. Please don't do this here.

https://news.ycombinator.com/showhn.html

No it doesn't "When something isn't good, you needn't pretend that it is.". I'm not using offensive language like "you are stupid" or similar. It is however a critique comment pointing out that the work is amateurish. Just look at it - text and code are full of false information and bugs. It's obvious it's written by somebody with very little experience.
You are right, the writing is wrong. I was trying to imply in the C++ class sense where we write functions directly inside a class, in Go, we have to define a function over a struct.

In your example: you call the method using an instance of T right? that was what I was saying, we have to define methods outside the struct declaration and then call it using an object of struct.

(*t).F(nil)

If t is nil, the function is still called with a nil value for t.
can we call the method without doing t.F()? like just F()?

I accept I have to modify the statement I made.

No. F refers to a different function than t.F.
Yes that was my point when i said we can't declare functions inside struct

We can have methods which work on objects on structs but never a function like class's method

(comment deleted)
The manning book uses vanilla Go too, but it's not free. However, it's a pretty good book.
It would be great to see anything on deployment added to this.
I know, but the thing is I have 0 practical experience in web app development, I work in the data warehousing field. So really have no idea about deployment, the only deployment I do is from the dev version of my to-do app to my own local version which I call Production :d
Thank you for this book! Tiny minor remark (which sometimes happens to me, too). Instead of:

    //If the password matches, return true
    if password == passwordFromDB {
        return true
    }
    //by default return false
    return false
This could simply be put as:

    //If the password matches, return true
    //by default return false
    return password == passwordFromDB
This is great! Now I'm wondering why it didn't strike me :D