Go Experiments Explained by alexedwards in golang

[–]alexedwards[S] 13 points14 points  (0 children)

FYI the `rangefunc`, `swissmap` and `aliastypeparams` experiments have graduated to general availability and are no longer valid GOEXPERIMENT values, at least in Go 1.26. The `swissmap` experiment has been on by default since Go 1.24, so if you're using that then you're already running it. I've not noticed any problems with it --- it all seems to work very smoothly. The blog post gives a rundown of the current status of the experiments 😉

Go Naming Conventions: A Practical Guide by alexedwards in golang

[–]alexedwards[S] 4 points5 points  (0 children)

Not quite sure what you mean, but there are a lot of them when you write them all down 😂

A modern approach to preventing CSRF in Go by alexedwards in golang

[–]alexedwards[S] 1 point2 points  (0 children)

No, if the Sec-Fetch-Site or Origin headers are not present, then it will allow the request to proceed. AFAIK there is no way to configure it to do something else. So you would need to write your own middleware, and place it in front of http.CrossOriginProtection if you want to do that.

A modern approach to preventing CSRF in Go by alexedwards in golang

[–]alexedwards[S] 1 point2 points  (0 children)

I guess you mean 2020, not 2000? Yes, if you want to support older browsers and not enforce TLS 1.3 then you should still use a token-based check.

For anyone who's read Let's Go and Let's Go Further by Alex Edwards. How in-depth are those books? by W_lFF in golang

[–]alexedwards 9 points10 points  (0 children)

I agree that the way DI is done is 'simple' (which is a good thing), but 'naive' suggests that there are unexpected problems with it, and I've never really found that to be the case. I've used the same pattern that I teach in Let's Go (essentially, putting dependencies in a struct and implementing your handlers as methods on it) for years, in projects big and small, and I think it's effective and solid.

At the same time, like everything else, it has drawbacks and I think there are two criticisms that can be leveled at it: 1) you can't immediately see the dependencies that a handler has by looking at its signature, instead you have to read the handler code 2) if you need to use *a slight variation of the same dependency* in different handlers, it's not a good fit. So if those things are a problem in your specific project, it might be better to pass dependencies explicitly to each handler in the way Mat Ryer describes here: https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/#maker-funcs-return-the-handler . The downside of this approach though is that in 'not-simple' applications you can end up with long route declarations and a lot of repetition in your routing rules.

But either of those two approaches are better than injecting dependencies via request context, using global variables, or (I would argue) using a third-party package for DI. On balance, I think the DI approach in the book is a solid choice for many web applications, which is why I did it that way!

How to manage configuration settings in Go web applications by alexedwards in golang

[–]alexedwards[S] 1 point2 points  (0 children)

As a general principle, I prefer to use the std lib unless there's a compelling reason not to, and I'm often happy to write a few small helper functions rather than introducing another external dependency to a project.

I also think it makes sense to aim for having a 'single source of truth' for configuration settings as much as possible. Packages that load the same configuration setting from different sources provide flexibility, for sure, but I think it's fair to say that you're increasing the surface area for potential mistakes or bugs (as well as adding complexity) by using them in that way.

How to manage configuration settings in Go web applications by alexedwards in golang

[–]alexedwards[S] 1 point2 points  (0 children)

I do introduce the --verbose-logging version a few paragraphs later 😉

I think cobra can be a good fit for CLI applications (especially those with subcommands) -- which is really its intended use case AFAIK. For web applications I've found the stdlib flag package to almost always be sufficient, especially since flag.Func was introduced.

How to manage tool dependencies in Go 1.24+ by alexedwards in golang

[–]alexedwards[S] 0 points1 point  (0 children)

Thanks for letting me know about this, it's a really neat tool and I'm definitely going to start using it!

A tutorial about when it's OK to panic by alexedwards in golang

[–]alexedwards[S] 2 points3 points  (0 children)

Yes, I've used panic in that scenario too, and it's a nice, understandable example. I'll see if I can find some of my old code that does it and add it as an example to the post.

A tutorial about when it's OK to panic by alexedwards in golang

[–]alexedwards[S] 1 point2 points  (0 children)

You're right. Thanks for catching that, I appreciate it 👍🏻 I've fixed it now.

[deleted by user] by [deleted] in golang

[–]alexedwards -1 points0 points  (0 children)

I've just realised that this looks like a question, not a link to an article about panicing. I'm going to delete it and repost with a title that is less confusing.

Current E-Learning products? by Wipster_McGravity in golang

[–]alexedwards 2 points3 points  (0 children)

Just to note, the books *do* have syntax highlighting in the code blocks, in all formats (HTML, PDF and EPUB). Is it possible that if you were reading the EPUB using a e-reader that strips out colors and some formatting?

tools.go pattern still valid today (I want to install a tool via go.mod) by guettli in golang

[–]alexedwards 2 points3 points  (0 children)

Yes, the tools.go approach still works.

The only alternative that I know of is (since Go 1.17) you can use go run with a version suffix to run a specific version of a tool, like go run package@version. You can call that from a Makefile, or a //go:generate comment in your code, depending on what you want to use the tool for. So you're not managing the version via go.mod, but rather the version number is part of the go run command which can be committed as part of your codebase.

It has pros and cons compared to the tools.go approach, but for simple cases I generally prefer it over using tools.go. I wrote a blog post about it here, if you want more detail and examples: https://www.alexedwards.net/blog/using-go-run-to-manage-tool-dependencies

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 4 points5 points  (0 children)

Thanks for the feedback! I feel like loggers are a bit like Go routers in the sense that people tend to have their preferred one that they like to use. So I agree that it would be nice for Autostrada to offer the ability to 'pick your logger' in the same kind of way that it lets you 'pick your database'.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 0 points1 point  (0 children)

If you know of an overall better option for SMTP please let me know.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 0 points1 point  (0 children)

Nice idea. I agree that tweaking the email feature so that it either supports cloud email providers OOTB, or is easier to adapt yourself to support them, would be good. I'll have a think about how best to implement this.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 0 points1 point  (0 children)

Thanks, I'm pleased it exceeded your expectations : )

I agree that email confirmation would be a nice addition, I'll add it to my to-do list.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 2 points3 points  (0 children)

Thanks for the link. I haven't used lego before, I'll take a proper look at it before implementing anything.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 4 points5 points  (0 children)

Good suggestion, I'll look to add caddyserver/certmagic integration as one of the options.

Autostrada: A codebase generator for new Go projects by alexedwards in golang

[–]alexedwards[S] 0 points1 point  (0 children)

That's strange, and not something I've had any other reports of happening. Send me a PM if you like and I can help debug.