
Show HN: Book about writing web apps in Go without a framework - thewhitetulip
https://github.com/thewhitetulip/web-dev-golang-anti-textbook/
======
eljimmy
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.

~~~
wanda
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](https://caddyserver.com)

~~~
colemickens
"the only thing" keeping you with nginx is virtually the most important thing
that I want out of my (reverse proxying) web server.

------
chrisbroadfoot
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...](https://thewhitetulip.gitbooks.io/webapp-with-golang-
anti-textbook/content/content/02.5.html)

> 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](https://play.golang.org/p/ER2r58_BRz)

~~~
thewhitetulip
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)

~~~
jamra
If t is nil, the function is still called with a nil value for t.

~~~
thewhitetulip
can we call the method without doing t.F()? like just F()?

I accept I have to modify the statement I made.

~~~
chrisbroadfoot
No. F refers to a different function than t.F.

~~~
thewhitetulip
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

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

~~~
bezzi
Don't you think that teaching how to use it with mysql or postegres would be
more useful than sqlite?

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

~~~
thewhitetulip
totally agree. +1

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

~~~
thewhitetulip
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!

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

~~~
unsignedqword
I feel old-fashioned for still preferring books printed on physical media...

~~~
thewhitetulip
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

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

~~~
thewhitetulip
> 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

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

~~~
thewhitetulip
Yes, I understand.

------
caleblloyd
You may want to mention something about Graceful restarts to avoid downtime
when upgrading. A great library to achieve this is:
[https://github.com/facebookgo/grace](https://github.com/facebookgo/grace)

~~~
thewhitetulip
Yes, I will add that :) Thanks for suggesting!

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

~~~
thewhitetulip
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

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

~~~
thewhitetulip
This is great! Now I'm wondering why it didn't strike me :D

------
kevindeasis
The manning book uses vanilla Go too, but it's not free. However, it's a
pretty good book.

~~~
thewhitetulip
Thank you

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

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

------
enahs-sf
It would be great to see anything on deployment added to this.

~~~
thewhitetulip
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

------
0xdeadbeefbabe
> 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?

~~~
thewhitetulip
I am sorry, I didn't understand your comment.

~~~
0xdeadbeefbabe
Why do you mention HN at all? Doesn't seem relevant. It's just a place where
people come to disagree.

~~~
thewhitetulip
Well I was angry, I see that there is no point of that line, I'll remove it
today.

