10 ms·
Web Development in Go
- hasenj 10y agoDoes go have any advantage over other compiled languages, like say, swift? For example: https://vapor.codes https://vapor.codes
- kevinburke 10y agoThat's a question with a pretty broad answer. Each language has its own set of tradeoffs and things that it values most highly. In particular Go is shaped by the experience of needing to run high-performance servers at a company with thousands of engineers; it was designed to address a lot of the problems with running C++ or Python in that environment. Swift came from Apple, and was designed to address a lot of the problems with writing mobile apps in Objective-C.
- kenshi 10y agoI don't think Swift was designed to address the problems of writing mobile apps in Objective-C. It seems more like a compiler/language developer's idea of what a good modern language should be.
- rokob 10y agohttp://carlosicaza.com/swiftbooks/SwiftLanguage.pdf http://carlosicaza.com/swiftbooks/SwiftLanguage.pdf Swift was designed for writing apps on the mac platforms, primarily on iOS, and although somewhat debatable as a replacement for Objective-C due to it's quirks. If you want proof of that, just read between the lines in the original documentation. Not to mention, many of the features that were called out originally are direct responses to issues with Objective-C: Optionals, Typed containers, Generics, modules, real first class functions instead of just blocks, final classes, let, etc.
- coldtea 10y ago>If you want proof of that, just read between the lines in the original documentation. Those things were obviously requirements for roles it should fulfill. But no things in its design hold it back from being a modern language that's even more expressive and fast than Go. Except some concurrency love perhaps.
- vram22 10y ago>it was designed to address a lot of the problems with running C++ or Python in that environment. Where have you heard/read the part about Python? Not saying you're wrong, just haven't heard of it. I had heard about the C++ part. I know of some general issues (e.g. static typing, perf), of course, but interested to know which ones Google had (with Python). Edit: The issues I've read about with C++ at Google (in Rob Pike's article) were, IIRC, C++'s complexity and long build times, even with distributed builds.
- russdpale 10y agoCan't speak for swift, but go is writing with asynchronous applications in mind.
- weberc2 10y agoI don't keep up with Swift, but last I checked it didn't have a mature, lightweight concurrency story. Speaking generally, I find that Go is easy to write (like a scripting language), but with static typing. I'm also not familiar with another language that is as easy to get up and running with (Go projects don't require you to learn a new project configuration file language, for example--just run `go build` and you get a binary).
- beat 10y agoThere are roughly a million different compiled languages throughout the history of computing. Every one of them has some sort of advantage over other languages. And of course, there are plenty of interpreted languages too. So the question is, what problem are you trying to solve? Does the language you choose address those problems effectively?
- Gonzih 10y agogo has very well established ecosystem to do low level networking in a very efficient manner. that is based on my experience. you can use go for different problems, but then tooling/libraries around that are not so great/stable. like gui programming for example, auido/video encoding/decoding.
- positr0n 10y agoIs the flash message setup a standard way implement that type of functionality?? Encrypting the cookie with a key that is delivered client side anyway seems like it doesn't buy you anything.
- kevinburke 10y agoThe key isn't sent to the client. You can set the flash message in plain text, but I'd rather avoid the possibility of tampering, and it lets me reuse the same cookie code I use to encrypt e.g. session tokens.
- deleted 10y ago[deleted]
- slrz 10y agoWhat kind of tampering are you thinking about here? Aren't those messages sent from the server to the client? Thus, in-transit tampering should be prevented by TLS. If the key isn't sent to the client, how is the client supposed to display that message? Why are these messages encoded into a cookie in the first place? I don't think I understand the concept.
- kevinburke 10y agoThe message is read on the server as part of the next request and then displayed in the appropriate spot in thr UI
- hasenj 10y agoThis kind of scheme is used by flask. If you have access to the cookies you can decrypt and read then but you can't change any of the content.
- weberc2 10y agoWhat's lacking in the HTTP library? The stock router isn't great, but it's pluggable. Also, serving static files has never been a problem http.StripPrefix("/tmp", http.FileServer(http.Dir("/tmp"))) (or something like that, I forget the details). I guess I didn't find it very hard to get into web development in Go with the standard library; I'm not sure there's enough friction to justify diving into third party libraries. :/
- kevinburke 10y ago> serving static files has never been a problem I address this in the post; there's no problem with it, but it requires that you (or your end users) come up with a solution for deploying static assets to production, figuring out the relative paths between the working directory and the asset directory, and ensuring that the right version of the binary loads the right version of the assets. An example of the type of difficulty here is that you couldn't remember how to do it off the top of your head. If you compile them into the binary, you (or your users) don't have these problems.
- beat 10y agoI thought compiling them into the binary was a very clever idea!
- AYBABTME 10y agoI've always assumed that compiling assets into the binary meant the http.FileServer can't use sendfile. I guess this means lower performance at scale, but I've never verified if this actually happened or its effect.
- vfaronov 10y agoLooking at Linux’s sendfile [1], it should be possible to use it like that in principle (but not with the standard http.FileServer, I guess). [1] http://man7.org/linux/man-pages/man2/sendfile.2.html http://man7.org/linux/man-pages/man2/sendfile.2.html
- 10y ago
- patrickmn 10y agoNaCl's secretbox for encrypting the session cookie - nice
- conroy 10y agoOn the subject of Go web development, I'll plug one of my favorite libraries: https://goji.io https://goji.io. It's a super-fast HTTP router that supports middleware and pattern matching.
- petepete 10y agoVestigo is another very good choice https://github.com/husobee/vestigo https://github.com/husobee/vestigo
- weinr0ck 10y agoI used to use http://goji.io/ http://goji.io/, but once 1.8 hit and context was part of the request object there was little use for this library. Also, maybe it was just me, but I couldn't do very advance routing with goji. So I switched back to gorilla mux
- brightball 10y agoThis is, IMHO, the PHP/Javascript problem showing up in Go. All 3 languages have so much bundled in for web development out of the box that it's too easy to create frameworks...so there are lots of them without any type of rallying standard. That's one of the reasons that I'm really averse to Go for web development that isn't just API based because you gain a lot from a complete, primary framework for web projects. It's one of the things that makes Elixir so appealing because you've got a clear next-gen-web winner in Phoenix with a language that brings you so much of what's good about Go due to it's Erlang/BEAM underpinnings.
- atarian 10y ago>Compared with Ruby, PHP, Python, or Javascript, you're going to get great memory and latency performance, and libraries that do what you expect. Isn't picking a language to solve memory/latency issues a pre-optimization? Also I'm not sure if one can argue that libraries from one particular language are better at doing what you expect than libraries from another language. Disclaimer: I'm trying to understand how much of what people are saying about Go is hype. People are telling me things like you can cross-compile on different platforms and output a binary, but I fail to see how these things are important for most API's and web apps.
- ksubedi 10y agoProbably an unpopular opinion but I don't think pre-optimization is a bad thing specially in use cases where you will need to optimize the application later, as refactoring things for optimization later is much more time consuming. That being said, it all depends on the use case. And about your point on binary output, its not just about being able to get one binary and running it in different platforms, its also about being able to run it without much setup in the future. For example, I had a hard time getting a nodejs app that I built a few years ago running again since so many libraries were deprecated and I had a lot of broken packages, but if it was built using Go, it would just be one binary with all the dependencies packaged in and I would be able to run it without any issues.
- jwilliams 10y agoI think people have come to interpret the pre-optimization mantra in different ways, which leads to some confusion. Personally I think it's fine to have optimization goals, particularly around things like performance, cost and security (or even your ability to hire engineers). Maybe you park some of them while you prove another point, but you can (and should) measure and adapt along the way.
- z3t4 10y agothis is why i only use pure JS in nodejs. then it will always work for that version of nodejs. you cant asume that binary version of nodejs will work forever though.
- meow_mix 10y agoHow good are the ORMs options in Go? One of the reasons Rails is my go-to for web development is how easy Active Record it makes configuration and changing databases.
- kevinburke 10y agoI'm not a big fan of Active Record (https://kev.inburke.com/kevin/faster-correct-database-queries/ https://kev.inburke.com/kevin/faster-correct-database-querie...) and tend to just write queries by hand. The benefit of this is you can Prepare them, which alerts you to syntax errors earlier and means the database doesn't have to parse the query each time you execute it. You also don't need to return every column if you know you only need an email address. I know people that have had success with gorm or sqlx.
- segmondy 10y agoHow many times have you changed databases in your production apps or are you building and selling software and wanting users to be able to pick their own database?
- ksubedi 10y agoI don't think that is the only reason to use ORM's. They definitely help me be more productive while building apps where super-high performance is not an issue.
- deckiedan 10y agoI really like the flexibility of being able to swap around dbs during initial development. At some point along the way once almost the whole major systems are working, I'll need to decide if I need FTS, sharding, to work on a shared host that already has MySQL / interact with a wordpress site, etc.
- nzoschke 10y agoThis weekend I played with the Buffalo framework [1] which includes "pop" [2] which an ORM of sorts. I really enjoyed working with both. It's nowhere near as magical as Rails, but it totally reduced the amount of code/boilerplate I needed to set up databases, migrations and to get Go "model" structs to and from the database. [1] http://gobuffalo.io/docs/getting-started http://gobuffalo.io/docs/getting-started [2] https://github.com/markbates/pop https://github.com/markbates/pop
- update 10y ago> You should write your next web server in Go Are people writing their own web servers these days? is that a thing? Seems like everyone uses nginx.
- vfaronov 10y ago“Your own web server” here doesn’t mean “your own program that reads from sockets and parses HTTP” — that is taken care of by Go’s standard library. “Your own web server” here means “your own program that serves HTTP requests”, and yes, people write programs like that all the time.
- update 10y agoI read the post. The language is just confusing. You shouldn't have to tell me that "You should write your next web server in Go" doesn't mean what it says on the tin. A web server is Apache, Nginx, Lighttpd, etc. What you just described is a Web Application, which I develop all the time on python -> uwsgi -> nginx stack. For the record, I'm not blaming you, but the author for the confusing language. The title says "Web Development in Go" -- web dev is traditionally web applications/html/css etc, not typically including servers. Then their opening sentence is "You should write your next web SERVER in Go" /pedant
- tps5 10y agoI don't think the language is the slightest bit confusing. I also think your definition of "web server" is wrong. I would say a web server is a program that processes HTTP requests and optionally responds with something (commonly HTML, but can respond with data of any format). That includes apache, nginx, etc. It also includes programs written in X language that process HTTP requests.
- fideloper 10y agoI personally prefer whatever language/framework gets value out the door quickest on the side of handling HTTP requests (e.g. rails or laravel for the front-facing items) and something like Golang for plumbing or work that requires speed/CPU optimization - queue workers, file manipulation, data streaming, whatever. Golang (and NodeJS) both feel really kludgy to me when getting into things like handling form submissions (validating input, redirecting with error/success messages, and all those tedious things that the popular frameworks abstract away the best they can).
- ksubedi 10y agoThis. After using Go, Nodejs (Express, Koa), PHP (Laravel), Java (Spring) and C# (Asp.Net Core), I realized that nothing beats me in productivity when I am using Spring or Asp.Net core when building a web app. Little things like form validation, route management, templating and a ton of little under the hood stuff are much much easier with older frameworks that have been around for a while.
- cutler 10y agoCan you expand on how Spring or Asp.Net are better than Laravel with respect to form validation, route management and templating which are generally regarded as PHP's fote?
- ksubedi 10y agoLaravel actually was great at form validation, route management and templating, probably one of the best, however performance is what made me move away from Laravel. With Spring and Asp.net Core, I got the best overall development experience compared to the other ones I mentioned.
- out_of_protocol 10y agoTry Elixir + Phoenix ;) All the little things you've mentioned plus more (like good asset management out of box, super easy to use channels, migrations, clustering, deep system inspection etc)
- Gonzih 10y agoWhat about templating, middlewares, security, authentication, authorization? Do I need to glue this from custom libraries to the web server myself every time I start a new project?
- kevinburke 10y ago- there's a templating example in the project - there are middleware examples in the blog post and in the project - depends on what you mean by "sucurity". there's an example of secure cookie encryption in the blog post and the project - depends on what sort of authentication or authorization you need. If you just need basic auth you can try https://godoc.org/github.com/kevinburke/handlers#BasicAuth https://godoc.org/github.com/kevinburke/handlers#BasicAuth
- Gonzih 10y ago- ok, i did not look at example, sorry - for logging, but thats not enough - thanks for pointing out typo in an interesting way, yeah but blogpost just mentions custom library that should be glued to your webserver - custom library again glued to the server on every project I would go for new projects with something different than go. Like scala. Go is great for rest apis, but once your app becomes more "complete" in terms of requirements go becomes more and more painful in my experience.
- kevinburke 10y ago> I would go for new projects with something different than go. You should probably write your own article then.