/r/golang
Ask questions and post articles about the Go programming language and related tools, events etc.
Post is not in keeping with an inclusive and friendly technical atmosphere.
Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
Productive communication requires effort. Think about how your words will be interpreted. Remember that sometimes it is best to refrain entirely from commenting.
In particular, respect differences of opinion.
Interpret the arguments of others in good faith, do not seek to disagree. When we do disagree, try to understand why.
Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation. Avoid unconstructive criticism: don't merely decry the current state of affairs; offer—or at least solicit—suggestions as to how things may be improved. Avoid snarking (pithy, unproductive, sniping comments) Avoid discussing potentially offensive or sensitive issues; this all too often leads to unnecessary conflict. Avoid microaggressions
What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
As a part of the Go community, this subreddit and those who post on it should follow the tenets laid out in the Go Code of Conduct: https://golang.org/conduct
Treat everyone with respect and kindness. Be thoughtful in how you communicate. Don’t be destructive or inflammatory.
Posts must be of interest to Go developers and related to the Go language.
This includes: - Articles about the language itself - Announcements & articles about open source Go libraries or applications - Dev tools (open source or not) specifically targeted at Go developers
We ask that you not post about closed-source / paid software that is not specifically aimed at Go developers in particular (as opposed to all developers), even if it is written in Go.
Do not post links to or instructions on how to get pirated copies of copyrighted material.
We have a monthly "Who's Hiring?" post that will stay pinned to the top of the subreddit. To avoid too much noise from companies, please post job openings there. Please keep in mind, this is for 1st party postings only. No 3rd party recruiters.
No GPT or other AI-generated content is allowed as posts, post targets, or comments. This is only for the content itself; posts about GPT or AI related to Go are fine.
As GPT content is difficult to distinguish from merely low-quality content, low-quality content may be removed too. This includes but is not limited to listicles and "Go tutorials" that have no human voice and add nothing of their own.
/r/golang
I’m searching for an easy to use validation library for Golang. It should have a sensible default validation errors and easy to extend
Hey everyone,
I'm seeking advice from experienced developers since we're more tinkerers than traditional developers, please don't judge our code too harshly!
We're two developers building an ambitious bootstrapped SaaS app.
After 2-3 years of development, our product is nearly mature enough for release, but we need to keep shipping features quickly.
However, we're noticing our backend code is becoming increasingly fragile. Our setup is a Go stateless REST API with a single Postgres database.
We've taken a pragmatic approach throughout development:
Single file
Split into multiple files (no folders)
handlers.go
main.go
config.go
...
Introduced packages by folders
|--- /router
| |--- router.go (route definitions)
| |--- table1.go
| |--- table2.go
| |--- ...
|
|--- /middlewares
| |--- middleware1.go
| |--- middleware2.go
| |--- ...
|
|--- /config
| |--- config.go (env variables etc.)
|
|--- /database
| |--- (all object models + migration management)
| |--- ...
|
|--- /utils
| |--- (helpers used across all API handlers for JSON parsing, standardize error returns...)
|
|--- /services
| |--- services.go (initializes all services)
| |--- data_migration.go (for managing data migrations)
| |--- stripe.go (billing)
| |--- emails.go (email sending for all API handlers)
| |--- table1.go (reusable methods for all API handlers)
| |--- table2.go
| |--- ...
|
|--- /lib
| |--- (external libs structs = AWS, Auth0, logger...)
|
|--- main.go (initializes all structs, handles dependency injection + starts server)
This structure has served us well until now!
It's been simple, with minimal context switching, few abstractions, and manageable complexity – perfect for shipping quickly and safely.
However, we're now facing several challenges:
type Service struct {
config *config.Config
utils *utils.HandlerUtils
Stripe *Stripe
aws *lib.AWS
productAnalytics *lib.ProductAnalytics
Emails *EmailsService
Users *UserService
Organizations *OrganizationService
//..
}
func NewServices(orm *lib.Orm, logger *lib.Logger, config *config.Config, stripe *Stripe, aws *lib.AWS, productAnalytics *lib.ProductAnalytics, utils *utils.HandlerUtils) *Service {
emailsService := &EmailsService{
orm: orm,
logger: logger,
config: config,
}
usersService := &UserService{
orm: orm,
logger: logger,
}
organizationsService := &OrganizationService{
orm: orm,
logger: logger,
}
//..
services := &Service{
config: config,
Stripe: stripe,
aws: aws,
productAnalytics: productAnalytics,
utils: utils,
Emails: emailsFunctions,
Users: usersService,
Organizations: organizationsService,
//...
}
services.Emails.services = services
err := emailsService.InitCronsEmail()
if err != nil {
logger.Error("couldn't init cron transactional emails:", err)
}
services.Users.services = services
services.Organizations.services = services
//...
return services
}
CRONs and data migrations feel out of place as services – should they be structured differently?
It's becoming unclear where to add shared code that needs different configurations across services
What would you recommend? We'd prefer to avoid introducing hexagonal architecture, Clean Code, or similar patterns, as they might slow us down and feel excessive for our needs.
Hi Gophers,
https://github.com/emiago/diago/releases/tag/v0.9.1
Diago as library aim is to be replacement for some current VOIP frameworks like Asterisk, where more control over media is needed.
Docs: https://emiago.github.io/diago/docs/getting_started/
Demo Examples: https://emiago.github.io/diago/docs/examples/
You can download binary and source distributions from the Go website:
https://go.dev/dl/
View the release notes for more information:
https://go.dev/doc/devel/release#go1.23.4
Find out more:
https://github.com/golang/go/issues?q=milestone%3AGo1.23.4
(I want to thank the people working on this!)
I'm building a web service that will connect to a DB. I'm toying with some ideas of how to maintain functionality while the DB is down, in case it ever goes down.
How would you do this? SQLite? Connect to a fallback clone? Cache things in ram until they can write again?
I've mostly been lucky enough in my career that I could just treat the DB as a given, and any hiccups didn't hurt too much, but I'm trying to build something that I want to be a bit more structurally sound.
Got any ideas?
Wrote a blog about this year's advent of code day 1. While solving the problem I was once again struck with the missing function for calculating absolute value for integers and decided to dig a lil deeper. You'll also find a small recap of how the abs()
function for floats evolved over time in the standard library.
https://www.bytesizego.com/blog/aoc-day1-golang
Please do give it a read and let me know your thoughts!
Hi again Gophers! 👋
After my Ultimate Makefile post, many of you said "This is great, but I use just!" Well, I heard you loud and clear. So here it is - the complete port of ultimate-gomake to a Justfile that's just as powerful (pun intended 😉).
For those who missed the original: this is a build system that grows with your project, from tiny experiments to enterprise-scale applications. And now you can choose your preferred flavour: Make or Just.
Same great features, new command runner:
The best part? If you're already using ultimate-gomake and want to switch, it's a drop-in replacement. All commands work the same way, just with just
instead of make
.
Original Makefile version: https://github.com/crazywolf132/ultimate-gomake
New Justfile version: https://github.com/crazywolf132/ultimate-gojust
Hello,
I am a newbie. I am using fedora and install go by using dnf install. Then in order to delete some packages, when I opened my home directory, I found folder named "go". I don't know why, suddenly panicked and deleted it using rm -rf. It didn't break anything, but then I decided to delete go completely and install again.
Now when I search the internet, I found that to delete go, I need to just delete go directory at /usr/local/go. And then, remove go related lines from /etc/profile and .bashrc. However, there is not any directory named "go" at /usr/local, and nothing is written related go at /etc/profile or .bashrc. When I run the code, go env GOROOT, it shows the location /usr. There is not anything related to go at /usr, too. When I run which go, it shows the location /usr/bin/go. There is not anything related go at there, too. However, go is installed, as I can use go command, write go version, and etc. How can I found the necessary directories and then delete go?
Ever since go 1.23 came out a few months ago, I've been practing writing iterators as they don't come as intuitive to me as I was hoping. I then decided to compile them all into one package called seqs and another seqs/transform for iterators that modify the input sequence (there's also a seqs2 and seqs2/transform2). I then put them all in a library that I've been calling goext as it's intended to 'extend' the go standard lib with iterators, common data structures and algorithms and more.
This is my first package intended for public use so I'm not confortable saying it's production ready just yet. I'd like to get some feedback from the community before I release version 1.0.
I'd appreciate any feedback.
I was doing a load test using Vegeta, and the results were very surprising. Java was nowhere close to Go, especially when it had to deal with a large chunk of memory.
Linking the java code (spring boot) and Go code as well.
Go Code:- https://www.mashupstack.com/share/674f89a0b93ff
Java Code:- https://www.mashupstack.com/share/674f8ba47134b
With results here
**Go Results**
echo "GET http://localhost:8080/fetch" | vegeta attack -duration=10s -rate=5000 | vegeta report
Requests [total, rate, throughput] 48827, 4999.68, 3334.51
Duration [total, attack, wait] 14.1s, 9.766s, 4.333s
Latencies [min, mean, 50, 90, 95, 99, max] 230.208µs, 1.67s, 1.249s, 4.105s, 4.564s, 5.239s, 8.343s
Bytes In [total, mean] 1128360, 23.11
Bytes Out [total, mean] 0, 0.00
Success [ratio] *** 96.29%***
Status Codes [code:count] 0:1812 200:47015
___________________________________________
*Java Spring Framework Result*
echo "GET http://localhost:8080/fetch" | vegeta attack -duration=10s -rate=5000 | vegeta report
Requests [total, rate, throughput] 29385, 2913.56, 48.25
Duration [total, attack, wait] 39.358s, 10.086s, 29.272s
Latencies [min, mean, 50, 90, 95, 99, max] 225.708µs, 16.132s, 26.193s, 30.057s, 30.095s, 30.253s, 30.421s
Bytes In [total, mean] 24687, 0.84
Bytes Out [total, mean] 0, 0.00
Success [ratio] *** 6.46%*** nowhere near to Go
Status Codes [code:count] 0:27486 200:1899
_______________________________________________________
Note the success ratio for Go and throughput which is insanely high in comparison to Java.
And note the wait time. It is on the very higher side of Java. Go out of the box just simply works.
It was the same program, the same machine on both sides and Go simply outperformed Java in my testing.
Let me know what are your thoughts.
Ok, so I know Go was made to be simple (and I fully encourage that). But pointer arithmetic is simple no ?
I mean it is simpler than extreme type conversion with unsafe package (I am having super weird lines of code that works but look awful 😖)
I've created a new request binder, compatible with net/http to bind requests (PathParams, QueryParams, Headers and Body) to Structs and Maps.
This is a partial rewrite of Echo binding methods, with some enhancements, to accept standard net/http requests.
Here is the repo:
https://github.com/gobigbang/binder
If you are interested, feedback is highly appreciated!
Hi fellow Gophers! 👋
You know that feeling when you're starting yet another Go project and thinking "ugh, time to copy-paste and modify that Makefile again"? Yeah, me too. That's why I built something I think you'll love - a Makefile that grows with your project and doesn't get in your way.
Here's what makes it special:
Here's the cool part - it's totally modular. Only want the basics? No problem. Need more features later? Just flip a switch. It's like LEGO for your build system.
Hey Gophers! 👋
I've just released SecretFetch, a Go library that makes working with AWS Secrets Manager as simple as using struct tags. No more hardcoding secrets or wrestling with complex AWS APIs!
Key Features:
Here's how simple it is to use:
type Config struct {
DBPassword string `secret:"database/prod/password"`
APIKey string `secret:"api/prod/key"`
}
// That's it! SecretFetch handles the rest
Check it out on GitHub: SecretFetch
Feedback and contributions welcome! 🙌
Hello!
Just started working with Pion and its amazing ! works great but with one caveat, for some reason the video stream gets stuck after 30 - 90 seconds (varies heavily), after 20 seconds or so it gets lagish and then slowly returns to normal, only to repeat this cycle after couple of minutes - over and over, and Im not sure why.
As this is not my first webRTC backend (altho with python), I tried to switch devices, using different browsers, and even having my own flutter app running on my iPhone and android all experiencing the same fate.
The Go server only takes the track the client stream, and returns it (like the reflect example) + I use web sockets with gorilla to trickle ICE .
Im running this on a big server (1tb ram and many cores) so resources is not a problem for the server (also checked the docker stats for spikes or anything like that resource utilization stays coherent). webrtc-internals shows that the server really stops sending frames the whole time (inbound are 0 FPS and bitrate - but outbound liked just fine).
I think I have some context issue or miss handling packets in some way, but im too new in go to understand it alone.
This is a repo to replicate the issue.
https://github.com/VerioN1/webrtc-poc-go
main2.go is experiencing it way worse (less than 20 secs to freeze) so you can play around with it.
Any help would be much appreciated
Hey! In case anyone here is thinking about implementing authorization in your Go lang apps - I wanted to share our OSS solution - Cerbos PDP.
(We just hit 3k+ stars) https://github.com/cerbos/cerbos and here’s our SDK + guide for implementing authorization in Go https://www.cerbos.dev/ecosystem/go
Here are some of Cerbos PDP’s key capabilities and updates:
PS. We just added an update that I think you’ll find valuable - The Cerbos server and the cerbosctl utility can now be installed on Nix environments using the new flake, and Go developers can now run Cerbos in-process using the new cerbos.Serve function from v0.39.0. This is useful for cases where running an external process is impossible or for running tests
Let me know what you think, if you have a moment.
I saw that the crypto/mlkem package was already implemented in the git repo, but not yet released (https://github.com/golang/go/issues/70122), does anyone know when the new go version is going to be released? I am developing an application that needs quantum-safe encryption such as the Kyber/ml-kem algorithm, therefore I am looking for golang libraries that implement quantum-safe key-pair cryptography. I appreciate any help, thank you!
Hi all,
Is there a consensus on what is best practice to use when encoding JSON to be sent to the client? Im using both techniques (struct w/ json tags) in my code right now, but wondering if i should just stick with one or the other
I've been struggling a good bit with picking up bubbletea which just always felt far more complicated than it needed to be. So while I don't claim to be an expert given that I had 4 days off and was stuffed like a turkey, I started blogging the various stages of my journey. If anyone finds this useful I thought I'd share.
I'll keep on adding chapters while I gain more insights. Any thoughts/feedback or revelation oh how to make writing a TUI easier in go, I'd love to hear from you.
I'm interested to know what sort of patterns or abstractions you guys are using to deal with the boilerplate.
Hey all,
I'm still learning go, sorry if this is dumb.
I am getting annoyed at functions that return multiple things and I really only care about the first. I want to be able to do quick dirty one liners instead of take in foo, _ := func()
then do something with foo
So after stumbling around I wrote this function
func First[T any](vals ...any) T {
return vals[0].(T) // unsafe, there's more checks in the real thing
}
I use it like this:
a := First[string](test_many()) // test_many returns (string, int, int, string)
fmt.Printf("%s\n", "hello "+a+" world")
Is there a way to get rid of the [string]
and the generic? If I go with only variadic arguments, without the generic, I wouldn't be able to take the result and just add another string to it, since I can't add a string and an any without a cast (as far as I'm aware)
I tried a reflection approach as well but that flopped in the same way.
Can I condense First() more so it's more intelligent about the types? I come from languages that lack reflection so this is new to me and I want to know what's possible / where the hiccups are.
I want to be able to write First(test_many())
and the return to be string, not an any.
In the example below, I was able to create a package with a single function. The package name is helloWorld
and the function name is HelloWorld
.
To call the function, I need to use helloWorld.HelloWorld()
. Is it possible to call the function by using helloWorld()
? The helloWorld
package is only going to have 1 function that will be made available for all other packages.
go.mod
module example.com/john/test
go 1.23.3
main.go
package main
import helloWorld "example.com/john/test/helloworld"
func main() {
helloWorld.HelloWorld()
}
helloworld/hello.go
package helloWorld
import "fmt"
func HelloWorld() {
fmt.Println("Hello World")
}
I am having a hard time understanding how range based for loops interact with structs that have slice fields. This was hard for me to figure out as it was a language misconception for me. I am relatively new in Go.
Consider this example with a struct and slice of this struct struct:
type MyStruct struct {
identifier int64
content []int
}
mySlice := make([]MyStruct, 0)
// here, added some elements to `mySlice`
later, when interacting with this using a range based for loop such as:
for _, elem := range mySlice {
elem.content = append(elem.content, 10)
}
Why is this not modifying the original elem.content
? Since slices are "reference types", i was expecting this to modify the original slice. I understand that normally range based for loops create copies, but I am confused as to why elem
is a deep copy and not a shallow copy(?). This was totally unexpected for me.
I guess my confusion also stems from the fact that when I assign one struct to another struct, the primitive variables are deep copied, but if there are any slice variables within the struct, they are shallow copied (slice descriptors are copied). I had another bug related to this in my code so I had to write a custom copier myself that does the deep copying work. I am confused because assignment seems to be different in behavior (i.e., does shallow copy) than this range based for loop behavior (i.e., does deep copy?). Am I missing something?
EDIT: AoC is advent of code
Title pretty much says it all. Obv using go, no frameworks or libs.
I’m pretty new to go and decided to use it for this years AoC and put my solutions up on github.
Anyone else doing it and has a repo they’re willing to share?
Edit: My repo so far https://github.com/scyence2k/AoC2024 (day 2 is unfinished). I know the solutions aren't great but any feedback is welcome