11 ms·
Go Web Examples
- gempir 10y agoThis is awesome. Examples are so important when learning something and I always found it hard to get good Go web examples
- sherlockgopher 10y agoThis might be helpful! Tutorial: https://github.com/thewhitetulip/web-dev-golang-anti-textbook/ https://github.com/thewhitetulip/web-dev-golang-anti-textboo... YouTube series: https://www.youtube.com/playlist?list=PL41psiCma00wgiTKkAZwJiwtLTdcyEyc4 https://www.youtube.com/playlist?list=PL41psiCma00wgiTKkAZwJ...
- ilozinski 10y agoGorilla mux is way overrated and overused. I'm not sure why it's the first choice for so many people. There are much better alternatives such as https://github.com/julienschmidt/httprouter https://github.com/julienschmidt/httprouter or https://github.com/gin-gonic/gin https://github.com/gin-gonic/gin.
- Arcaire 10y agoCare to explain why it's overused in comparison to other toolkits/frameworks/libraries? Taking in to account, of course, the target use-cases.
- AsyncAwait 10y agoPart of the reason is that gorilla/mux uses identical API to the standard library.
- iamgopal 10y agoContradiction does not exist. :)
- sethammons 10y agoI avoid httprouter because it doesn't adhere to the standard library interface for http handlers. I've not used gin, but it looks to also not follow the standard library interface here. Also, it does not appear to make use of newer developments in Go such as the context package (it imports the x/pkg version but does nothing with it I think).
- joncalhoun 10y agoI believe gorilla mux came out years before the others, so it's not really shocking that it is used more. I personally have never had any issues with it but to each there own.
- mmgutz 10y agoAlong the same tone, httprouter doesn't allow defining these routes simultaneously users/:id users/watching Was kind of surprised as we have many routes like that. Like gorilla/mux, I think it was just first (trie-based mux). Isn't gin based on httprouter? There are even better muxes now. `pressly/chi` is used by heavy hitters in production and takes advantage of Go 1.7 HTTP context. `labstack/echo` is another highly recommended by others but I don't like the non-idiomatic echo.Context in handler signatures. Back on topic, I hope the go web examples only imports built-in packages. The gorilla/mux example could easily be written to use built-in packages.
- jhoechtl 10y agoI am currently searching for an example / skeleton in Go where I can upload a file via the browser to the server (drag and drop added bonus). Any examples?
- merlish 10y agoI actually wrote an extremely shonky/basic file upload thing and contributed it to the weird 'illacceptanything' repository, while that was a thing, about a year and a half ago. Maybe you'll find it helpful, maybe not. Ignore the bindata.go stuff and look at main.go (inc. file accepting) & the file upload control (w/Bootstrap fileinput.min.js) & the braindead simple HTML templating stuff. https://github.com/leafi/illacceptanything/commit/1b78922f891e455d56df9632740e3f9a62ee6009 https://github.com/leafi/illacceptanything/commit/1b78922f89...
- lesiki 10y agoI don't know much about Go so can't speak to the server-side handling of file uploads. The drag/drop side of things is server agnostic - I strongly recommend http://www.dropzonejs.com http://www.dropzonejs.com
- lentil 10y agoThis might be more of a complete app than you need, but https://transfer.sh https://transfer.sh is open source and it looks fairly compact. Could be some interesting examples in there?
- Spiritus 10y agoSomething like this will hopefully get the point across on how to handle "multipart/form-data" forms. It will print the content of the upload to the server's stdout. https://play.golang.org/p/DiNGdl27kE https://play.golang.org/p/DiNGdl27kE You can use curl to test it out (it assumes a file called "foobar.txt" is available): curl -v -F upload=@foobar.txt localhost:8080/upload
- bitexploder 10y agoI really like the gophish app for easy to understand common patterns in web apps.
- mreithub 10y agoHas anyone of you experience with the fasthttp Go library[0]? Does it keep its promise in terms of speed? (I assume real world code would spend much more time in actual business logic than their examples/benchmarks so I guess there wouldn't be as much a difference as they claim, but the idea of zero allocations intrigues me) [0]: https://github.com/valyala/fasthttp https://github.com/valyala/fasthttp
- grey-area 10y agoIt doesn't support http2.
- tscs37 10y agoThe fasthttp library is good if you don't need to support corner cases of HTTP and you don't need HTTP/2. If that is given, yes, it's faster.
- daenney 10y agoIf you want HTTP/2 support Chi[0] can probably help. It's plenty snappy too. [0]: https://github.com/pressly/chi https://github.com/pressly/chi
- sagichmal 10y agoI don't recommend it; it doesn't actually implement HTTP, and the stdlib package net/http is extremely fast on its own.
- petepete 10y agoThese look great, thanks. I'm currently learning Go and find this kind of example much easier to follow than documentation. I'd like to see an example for embedding HTML templates properly. It took me an embarrassing amount of time to get beyond 'header.html' and 'footer.html' being separate, and I'm sure my solution isn't as elegant as it could be.
- joncalhoun 10y agoIs https://www.calhoun.io/creating-the-v-in-mvc/ https://www.calhoun.io/creating-the-v-in-mvc/ close to what you had in mind? I need to do a follow-up to that with the type I use to wrap that logic, but I am curious if this is inline with what you are looking for in the examples.
- petepete 10y agoYes, thank you. My solution was similar but less elegant; I plan on going through your post in more depth and attempt to reimplement my code with what I've learned. Once again, thank you for that post.
- tlow 10y agoWhy put "<li><s>" and "</s></li>" in the html file? Given your style you should clearly abstract this out static HTML. Edit: update. Odd that people are upvoting and downvoting this comment.
- nkozyra 10y agoPossibly because the underlying HTML is not really the point of the tutorial.
- tlow 10y agoIn web examples HTML is "not really the point"? Ok. I can actually accept that as reasonable. But isn't the style of this example to abstract static HTML out of the HTML file and generate it dynamically as needed? I'll grant that it's a rather small issue; nonetheless, I do think the author would be best served by said abstraction for style and consistency (to the last detail).
- adamnemecek 10y agoActually the link should be to https://gowebexamples.github.io/ https://gowebexamples.github.io/ I screwed up when posting this lol.
- dang 10y agoOk, we changed to that from https://gowebexamples.github.io/templates/ https://gowebexamples.github.io/templates/.
- azinman2 10y agoFantastic. I've found previous examples hard to get into... either way too complex, not doing enough, or not structured well. Thank you! Update: 2 requests: http/2, and https (self-signed, with chained cert, and mutual auth)
- sherlockgopher 10y agoI found this to be a good source, https://github.com/thewhitetulip/web-dev-golang-anti-textbook/ https://github.com/thewhitetulip/web-dev-golang-anti-textboo...
- resist_futility 10y agoanother set of examples https://gist.github.com/denji/12b3a568f092ab951456 https://gist.github.com/denji/12b3a568f092ab951456
- bharani_m 10y agoI am interested in trying out Go for my next side project. Can anyone recommend any good books/videos that can help me get started?
- beliu 10y agoAnother great resource for getting started is https://gobyexample.com/ https://gobyexample.com/. Also check out usage examples on Sourcegraph (disclaimer: I'm the CTO). We sell our tool to companies to use on their private code, but a side effect is that we've indexed usage examples for just about every popular Go open-source library. For example, - Go std lib http package: https://sourcegraph.com/github.com/golang/go@bb41b4d599f5758e25091666e123c41b401ac890/-/blob/src/net/http/request.go#L758:6-758:16 https://sourcegraph.com/github.com/golang/go@bb41b4d599f5758... - Popular gorilla/mux routing library: https://sourcegraph.com/github.com/gorilla/mux@34bf6dc9faa08144e925df4fa1838551b16d6c2a/-/blob/mux.go#L17:6-17:15 https://sourcegraph.com/github.com/gorilla/mux@34bf6dc9faa08... - Gorp, a popular ORM-ish library: https://sourcegraph.com/github.com/go-gorp/gorp@033bf796a22fc4f028c72754ca00e124e25d2928/-/blob/gorp.go#L98:6-98:17 https://sourcegraph.com/github.com/go-gorp/gorp@033bf796a22f... Cross-repo jump-to-def + find-references = great for making sense of new code.
- sethammons 10y agoI would replace gorp with https://github.com/vattle/sqlboiler https://github.com/vattle/sqlboiler. Granted, I've not used either, but I like the philosophy behind sqlboiler. I feel it is more in line with Go principles. I also believe gorp will have issues with the new DB context stuff (though I'm not positive on that).
- adamnemecek 10y agoI know this isn't a book but if you need a go development environment, cloud9 (https://c9.io https://c9.io) ended up being a surprisingly productive environment. Like for go development, it's been better than anything I've tried to setup locally and the IDE even has go autocomplete.
- sherlockgopher 10y ago
- pkulak 10y agoLove the way that library handles websockets. Perfect example of the way Go tends to work: no magic at all, yet it's also simple, concise and elegant.
- Shanea93 10y agoThis is great, really simple and easy to follow examples. Would you be able to add some other examples such as interfacing with a database or making remote network requests over http?
- blackss2 10y agoIf you wanna simple database utility for testing golang db conn, please check https://github.com/blackss2/utility/blob/master/README.md https://github.com/blackss2/utility/blob/master/README.md go get github.com/blackss2/utility/convert * don't use for production
- elmacho39 10y agoAs always (not go specific), forms example is missing form validation. Is that too old-fashioned?
- fallenhitokiri 10y agoWhat kind of validation are you looking for? Simple things like "is not empty", "maximum 8 characters"...? Most libraries I've seen which try to do form validation always lack in certain areas or force you to define your own validation, so I simply ended up adding an `isValid` method to my structs.
- elmacho39 10y agoI want to see a full roundtrip: 1. define input fields in html template of choice 2. validate form/struct/whatever and prepare errror messages per field 3. redisplay html with field-level errors to let user correct its input step 3. is the interesting and missing in most examples of newer webframeworks
- jchannon 10y agoGo Content Negotiation - https://github.com/jchannon/negotiator https://github.com/jchannon/negotiator
- StevePerkins 10y agoIt seems like there are three classes of "web problems" to solve: (1) The absolute basic "hello world" stuff at the outset. How to route URL's to their handlers, how to render dynamic templates, how to map data structures to and from JSON, etc. (2) The problems that are one level up in sophistication, but still common to nearly all web apps. How to handle authentication (username/pass, OAuth/JWT, etc), how to manage session state or the lack thereof, juggling synchronous blocking operations with async background ones, etc. (3) Really advanced issues that are probably specific to your industry domain or specific application, and aren't necessarily common to others (e.g. how to isolate this medical data for HIPPA compliance). I don't want to poo-poo this website, it's nice. But in my mind, the world is already flooded with Category #1 examples. At the same time, Category #3 doesn't really lend itself to this sort of thing at all. So what the world really needs is more Category #2 stuff. Example: What are the patterns available for authentication, including their advantages and drawbacks? If you do your own authentication with usernames and passwords... then you have to either have to pass them every time and re-auth every request, or else store a session cookie on the client side and find some way share it across all your server-side instances (or use a load balancer with sticky sessions). That's material for 3 or 4 patterns right there! If you use OAuth with JWT tokens, then you can (maybe) avoid the need to store any session state on the server side. But how do you explicitly "logout" prior to the JWT expiration? Another 3 or 4 patterns down this route! Category #1 is fun and easy to write about, and a lot af that material comes from newbies who are writing in order to teach themselves. But Category #2 is the brick wall that people always run into in the real world... and because you need a lot of real experience to write about that class of problems, there's a vacuum of available material out there.
- tptacek 10y agoApart from the JWT thing (please avoid JWT if possible), I agree with this comment 100%. Things I'd like to see in a Go web app example site: 1. Examples of using context values to pass things selectively to handlers. 2. An arbitrary trivial middleware example, like a specialized request logger, just to demonstrate Go middleware. 3. A Rails/Django-style session token done in the same middleware style. 4. Giving requests a notion of "current user". 5. Simplified user-visible vs. developer-visible error handling. It's too much to ask for ORM-style database query examples, I know, but that too would be nice.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- omginternets 10y agoAnybody have experience with Echo? [0] [0] https://echo.labstack.com/ https://echo.labstack.com/
- nathancahill 10y agoYes, I use it for several microservices, running for about a year now in prod. It's excellent.
- omginternets 10y agoIt seems very nice indeed. Thanks for the feedback.
- amelius 10y ago> This example will show how to register a route and get the data using just the net/http package. What is a "route"? (I don't need an answer, but I think the example should explain it).
- nnd 10y agoWhy would I want use Go for the backend, rather than a Python or Node.js framework? Just looking at the syntax it strikes me how inconcise and it is.
- falcolas 10y ago> Just looking at the syntax it strikes me how inconcise and it is. And you recommend Javascript instead? Interesting. > Why would I want use Go for the backend Fast, built-in concurrency, enough high level concepts and structures to support fast development, and really easy to deploy. The last part is probably the greatest. It supports simple host-based deploys, tiny docker container deploys, and eliminates the typical dependency conflict problems of Python and Node. It's also pragmatic enough to make it easy to develop in for developers across the skill bell curve.
- simplyluke 10y agoIn general: more performant, much better at threading through goroutines. It's worth noting go is compiled, so it's more comparable to a language like C++ than a dynamic language like python or JS.
- my_ghola 10y agoI haven't tried Go in a while but I see a lack of error handling in the code examples. Is that just to keep the code simple? I'd rather see examples with best practice handling of errors. Maybe show both versions so readers can see the difference?
- hit8run 10y agoThese guides go into the right direction. I solved many things for myself but it would be nice to collect good practices. I had to come up with go style design patterns for: - Middlewares - Authentication - Authorization - Database Access - JSON APIs - Template organization - Migrations - i18n - Crypted Cookies - Image uploading - Form validation - Asset Pipeline - API Versioning - Writing libs for 3rd party services Where is the best place to share my best practices and to discuss them with others? Via a pull request to that repo? You know of other good sources?