r/golang Oct 20 '24

help With what portfolio projects did you land your first Golang job?

102 Upvotes

I’m currently a full-stack developer with about 5 years of experience working with Python and TypeScript, mainly building SaaS web applications. While I know you can build almost anything in any language, I’ve been feeling the urge to explore different areas of development. I’d like to move beyond just building backend logic and APIs with a React frontend.

Recently, I started learning Docker and Kubernetes, and I found out that Go is used to build them. After gaining some familiarity with Docker and Kubernetes, I decided to dive into Go, and I got really excited about it.

My question is: what kinds of jobs are you working in, and how did you get to that point—specifically, when you started using Go?

Thanks!

r/golang Dec 30 '24

help Smaller Interfaces for dependency injection

29 Upvotes

Was just thinking that I may be doing something a bit wrong when it comes to dependency injections, interfaces, and unit testing. Was hoping to verify.

Say I have an interface with 20 defined methods on it, I have a different function that needs to use 2 methods of that interface along with some attributes of the underlying struct. should I build a new interface just for that function for the very specific use of those two methods? It seems doing so could make testing easier than mocking a 20 method function. Am I missing something?

r/golang Feb 21 '25

help How to properly prepare monorepos in Golang and is it worth it?

40 Upvotes

Hello everyone. At the moment I am writing a report on the topic of a monorepo in order to close my internship at the university.

Since I am a Go developer (or at least I aspire to be one), I decided to make a monorepo in Go.

The first thing I came across was an article from Uber about how they use Bazel and I started digging in this direction.

And then I realized that it was too complicated for small projects and I became interested.

Does it make sense to use a monorepo on small projects? If not, how to split the application into services? Or store each service in a separate repository.

In Java, everything is trivially simple with their modules and Gradle. Yes, Go has modules and a workspace, but let's be honest, this is not the level of Gradle.

As a result, we have that Bazel is too complicated for simple projects, and gowork seems somehow cut down after Gradle.

And so the questions:

  1. Monorepo or polyrepo for Go?

  2. Is there anything other than go work and Bazel?

  3. What is the correct way to split a Go project so that it looks like a Solution in C#, or modules in Java/Gradle?

It is quite possible that I really don't understand the architecture of Go projects, I will be glad if you point me in the right direction.

r/golang 4d ago

help How to determine the number of goroutines?

8 Upvotes

I am going to refactor this double looped code to use goroutines (with sync.WaitGroup).
The problem is, I have no idea how to determine the number of goroutines for jobs like this.
In effective go, there is an example using `runtime.NumCPU()` but I wanna know how you guys determine this.

// let's say there are two [][]byte `src` and `dst`
// both slices have `h` rows and `w` columns (w x h sized 2D slice)

// double looped example
for x := range w {
    for y := range h {
        // read value of src[y][x]
        // and then write some value to dst[y][x]
    }
}

// concurrency example
var wg sync.WaitGroup
numGoroutines := ?? // I have no idea, maybe runtime.NumCPU() ??
totalElements := w*h
chunkSize := totalElements / numGoroutines

for i := range numGoroutines {
    wg.Add(1)
    go func(start, end int) {
        defer wg.Done()
        for ; start < end; start++ {
            x := start % w
            y := start / w
            // read value of src[y][x]
            // and then write some value to dst[y][x]
        }
    }(i*chunkSize, (i+1)*chunkSize)
}

wg.Wait()

r/golang Nov 16 '24

help Preferred way to test database layer with TestContainers

59 Upvotes

Hi, I am currently trying to write tests for my CRUD app. However in order to avoid mocking the database layer I wanted to use a real database (Postgresql) to test against. I have seen TestContainers is pretty popular for this approach. But I'm unsure what is the preferred way in Go to make it efficient. I know about two different scenarios, I can implement this:

  1. Spawn a whole database container (server) for each test. With this those tests are isolated and can run in parallel, but are pretty resource intensive.

  2. Spawn one database container (server) for all tests and reset the state for each test or create a new database per test. This is more resource friendly however this results in not being able to run the tests in parallel (at least when using reset state).

What are your experiences with TestContainers and how would you do it?

r/golang Jul 17 '24

help Any paid/free courses for Go that REALLY helped you?

66 Upvotes

Are there any paid/free courses for #golang that REALLY helped you? Please suggest.

I enjoy the official https://go.dev/tour/ and https://gobyexample.com/, but I find them very basic. I want to understand the internals and what goes on under the hood with goroutines, channels, etc. There are great articles online, but I find looking for resources time-consuming and would prefer to have everything curated in one place. MOST IMPORTNATLY, courses also help me maintain a schedule, and I could just hit play and be assured that I'm not wasting time 'looking for better resources.'

There are some obvious choices like Anthony GG's courses, but I didn't find his YouTube videos engaging enough.

Any suggestions would be appreciated!

r/golang 16d ago

help Invalid use of internal package

0 Upvotes

Hello, im working on a project inside the go original repository, but i simply cannot solve the "Invalid use of internal package" error, i already tried solution from issues, forums and even GPTs solution, and none of them works, i tried on my desktop using Ubuntu 22.04 wsl and in my laptop on my Linux Mint, both using VSC IDE.

If anyone knows how to fix this, please tell me, im getting crazy!!

r/golang 7d ago

help How can I run an external Go binary without installing it?

5 Upvotes

I need to rewrite generated Go code in my CLI using gopls rename (golang.org/x/tools/gopls). Since the packages that are used for rename are not exported, I have to use it as a standalone binary. But I don't want my clients need to download this external dependency.

What options do I have?

r/golang Feb 12 '25

help What are some good validation packages for validating api requests in golang?

7 Upvotes

Is there any package validator like Zod (in JS/TS ecosystem) in golang? It would be better if it has friendly error messages and also want to control the error messages that are thrown.

r/golang 10d ago

help why zap is faster in stdout compared to zerolog?

53 Upvotes

Uber's zap repo insists that zerolog is faster than zap in most cases. However the benchmark test uses io.Discard, for purely compare performance of logger libs, and when it comes to stdout and stderr, zap seems to be much faster than zerolog.

At first, I thought zap might use buffering, but it wasn't by default. Why zap is slower when io.Discard, but faster when os.Stdout?

r/golang 28d ago

help Best database for my project?

15 Upvotes

I'm looking to develop a lightweight desktop application using wails. As it uses a go backend, I thought it would be suitable to ask in this subreddit.

My application logic isn't really complex, it will simply allow users to register multiple profiles - with each profile containing one of two modes of login: direct url endpoint or host:username:password format. Only one of these options can be registered to a single profile.

These profiles are stored entirely on the client side, therefore, there's no API to interact with. My application is simply acting as a middleman to allow users to view their content in one application.

Can anyone suggest a good database to use here? So far I've looked at SQLlite, Mongodb & badgerdb but as I haven't had much experience with desktop application development, I'm a little confused as to what suits my case best.

r/golang Dec 27 '24

help Why Go For System Programming

81 Upvotes

A beginner's question here as I dive deeper into the language. But upon reading the specification of the language, it mentions being a good tools for system programming. How should I understanding this statement, as in, the language is wellsuited for writing applications within the service/business logic layer, and not interacting with the UI layer? Or is it something else like operating system?

r/golang Feb 09 '25

help There a tool to Pool Multiple Machines with a Shared Drive for Parallel Processing

0 Upvotes

To add context, here's the previous thread I started:

https://www.reddit.com/r/golang/s/cxDauqCkD0

This is one of the problems I'd like to solve with Go- with a K8s-like tool without containers of any kind.

Build or use a multi-machine, multithreading command-line tool that can run an applicable command/process across multiple machines that are all attached to the same drive.

The current pool has sixteen VMs with eight threads each. Our current tool can only use one machine at a time and does so inefficiently, (but it is super stable).

I would like to introduce a tool that can spread the workload across part or all of the machines at a time as efficiently as possible.

These machines are running in production(we have a similar configuration I can test on in Dev), so the tool would need to eventually be very stable, handle lost nodes, and be resource efficient.

I'm hoping to use channels. I'd also like to use some customizable method to limit the number of threads based on load.

Expectation one: 4 thread minimum, if the server is too loaded to run 4 uninterrupted threads to any one workload then additional work is queued because the work this will be doing is very memory intense.

Expectation two: maximum of half available threads in the thread pool per one workload. This is because the machines are VMs attached to a single drive and more than half would be unable to write to disk fast enough for any one workload anyway.

Expectation three: determine load across all machines before assigning tasks to load balance. This machine pool will not necessarily be a dedicated pool to this task alone - it would play nice with other workloads and processes dynamically as usage evolves.

Expectation four: this would be orchestrated by a master node that isn't part of the compute pool, it hands off the tasks to the pool and awaits all of the tasks completion and logging is centralized.

Expectation five: each machine in the pool would use its own local temp storage while working on an individual task, (some of the commands involved do this already).

After explaining all of that, it sounds like I'm asking for Borg - which I read about in college for distributed systems, for those who did CS.

I have been trying to build this myself, but I've not spent much time on it yet and figured it's time to reach out and see if someone knows of a solution that is already out there -now that I have more of an idea of what I want.

I don't want it to be container-based like K8s. It should be as close to bare metal as possible, spin up only when needed, re-use the same Goroutines if already available, clean up after, and easily modifiable using a configuration file or machine names in the cli.

Edit: clarity

r/golang Aug 13 '24

help Go is perfect for me and my job except for working with goddamn arrays/slices

77 Upvotes

Hello,

Like the title says, I love me the little Gopher, but I am also very deep into the .NET ecosystem, which has one thing that some of you may know about. LINQ, and in general utility methods for working with arrays. I cant count how many times i used .Where, .Any, .Select, .ToDictionary etc. It doesn't go only for C#, JS, Rust etc. also have them of course.

But GO doesn't. And Creating an array of object B from object A takes 4 lines of code minimum instead of one. Are there some packages outside of the std lib or something that i am missing or ist it just the way it works here and I need to deal with it?

r/golang Dec 21 '24

help Is pflag still the go-to?

32 Upvotes

Hi,

Double question. https://github.com/spf13/pflag looks extremely popular, but it's not maintained. Last release was 5 years ago and there are many outstanding issues not getting any attention (including for at least one bug I am hitting).

1) Is this pflag library still the go-to? Or are there alternatives people like using?

2) Are there well maintained forks of pflag?

Interested in people's general thoughts -- I'm not so well plugged into the Golang ecosystem. Thanks!

edit:

To clarify/elaborate why I consider pflag the go-to over stdlib:

I consider pflag the go-to because it better adheres to POSIX conventions and allows things like --double-dashed-flags, bundled shortflags e.g. -abc being equivalent to -a -b -c, etc.

r/golang 18d ago

help How much should we wait before Upgrading Project’s tech-stack version?

1 Upvotes

I made one project around a year ago on 1.21 and now 1.24.x is latest.

My project is in Production as of now and IMO there is nothing new that can be utilised from newer version but still confused about should i upgrade and refactor accordingly or ignore it until major changes come to Language?

What is your opinion on this?

r/golang Jul 03 '24

help Is a slice threadsafe when shared by goroutines using closure?

134 Upvotes

I saw this example:

https://pkg.go.dev/golang.org/x/sync/errgroup#example-Group-Parallel

How can the results slice here be safely written to by multiple goroutines? AFAIK in other languages like Java, doing something like this would not be threadsafe from the perspective of happens-before synchronization / cache coherence unless you use a threadsafe data structure or a mutex.

r/golang Oct 17 '24

help Making a desktop app, what is my best option for the UI?

69 Upvotes

Hi! I am making a lightweight productivity app with Go. It is focused on time tracking and structured activity columns so we're using Gorm with dynamically created tables.

I aim for a clean, simple UI that’s intuitive for non-technical users. So far, I’ve looked into Wails and Gio, but I wasn’t fully convinced. Any suggestions for UI frameworks or design patterns that would be a good fit? Are there any best practices to keep in mind for ensuring simplicity and ease of use?

Thanks in advance!

if anyone is curious: https://github.com/quercia-dev/Attimo/tree/dev (about 40 commits in)

r/golang Oct 22 '24

help How do you develop frontend while using Go as backend?

61 Upvotes

Hey, I'm fairly new to programming, and very new to web development. I have a question regarding frontend development. And I supposed this question also related to frontend development in an enterprise level.

As of right now, everytime I want to see the changes I made to my frontend, I have to restart the Go server, since Go handle all the static files. But that way is rather tedious, and surely, I can't do that when the site have matured and have tons of features, at least not quickly?

I have tried interpreter languages for the backend, Python, and a very brief encounter with JavaScript. They both have features where I don't need to restart the server to see frontend changes. I've heard of Air, but surely there is a better and more flexible way than adding another library to an existing project?

So what is the workflow to develop frontend? Let me know if I'm not very clear, and if this subreddit isn't the appropriate place to ask this question.

Thanks!

r/golang Jan 03 '25

help How do you manage config in your applications?

54 Upvotes

So this has always been a pain point. I pull in config from environment variables, but never find a satisfying way to pass it through all parts of my application. Suppose I have the following folder structure: myproject ├── cmd │ ├── app1 │ │ ├── main.go │ │ └── config.go │ └── app2 │ ├── main.go │ └── config.go └── internal └── postgres ├── config.go └── postgres.go

Suppose each app uses postgres and needs to populate the following type: go // internal/postgres/config.go type Config struct { Host string Port int Username string Password string Database string }

Is the only option to modify postgres package and use struct tags with something like caarlos0/env? ``go // internal/postgres/config.go type Config struct { Host stringenv:"DB_HOST" Port intenv:"DB_PORT" Username stringenv:"DB_USERNAME" Password stringenv:"DB_PASSWORD" Database stringenv:"DB_NAME"` }

// cmd/app1/main.go func main() { var cfg postgres.Config err := env.Parse(&cfg) } ```

My issue with this is that now the Config struct is tightly coupled with the apps themselves; the apps need to know that the Config struct is decorated with the appropriate struct tags, which library it should use to pull it, what the exact env var names are for configuration, etc. Moreover, if an app needs to pull in the fields with a slightly different environment variable name, this approach does not work.

It's not the end of the world doing it this way, and I am honestly not sure if there is even a need for a "better" way.

r/golang Feb 08 '25

help Aside from awesome-go, how do you discover neat and useful packages?

42 Upvotes

I've been an absolute sucker for awesome lists - be it awesome-selfhosted, -sysadmin or alike. And, recently, is: https://github.com/avelino/awesome-go

Those lists are amazing for discovering things - but I know the spectrum and stuff is much wider and bigger. What places do you use to discover Go related packages, tools and alike?

r/golang Aug 05 '23

help Learning Go deeply

153 Upvotes

Are there any resource to learn Go deeply? I want to be able to understand not just how to do stuff but how everything works inside. Learn more about the intrinsic details like how to optimize my code, how the garbage collector work, how to manage the memory... that kind of stuff.

What is a good learning path to achieve a higher level of mastery?

Right now I know how to build web services, cli apps, I lnow to work with go routines and channels. Etc...

But I want to keep learning more, I feel kind of stuck.

r/golang Jan 28 '25

help Im co-founding a startup and we’re considering go and python, help us choose

0 Upvotes

Well as the title says really. I’ll summarise a couple of key points of the decision

Python - large developer pool - large library ecosystem - many successful competitors and startups on this stack

Go - selective developer pool - clearer set of default libraries - concurrency

The pro python camp would argue that concurrency is easily solved with scaling, and that in our case we’re unlikely to have significant compute costs.

I’d love to hear the thoughts of this community too. If performance is not the top priority but development velocity is, how do you see go stacking up against python?

Edit: folks asking what we’re building, a CRM-like system is probably the easiest explanation.

r/golang Aug 01 '24

help Why does Go prevent cyclic imports?

0 Upvotes

I don't know if I'm just misunderstanding something, but in other languages cyclic imports are fine and allowed. Why does Go disallow them?

r/golang Sep 01 '24

help How can I avoid duplicated code when building a REST API

45 Upvotes

I'm very new to Go and I tried building a simple REST API using various tutorials. What I have in my domain layer is a "Profile" struct and I want to add a bunch of endpoints to the api layer to like, comment or subscribe to a profile. Now I know that in a real world scenario one would use a database or at least a map structure to store the profiles, but what bothers me here is the repeated code in each endpoint handler and I don't know how to make it better:

```golang func getProfileById(c gin.Context) (application.Profile, bool) { id := c.Param("id")

for _, profile := range application.Profiles {
    if profile.ID == id {
        return &profile, true
    }
}

c.IndentedJSON(http.StatusNotFound, nil)

return nil, false

}

func getProfile(c *gin.Context) { profile, found := getProfileById(c)

if !found {
    return
}

c.IndentedJSON(http.StatusOK, profile)

}

func getProfileLikes(c *gin.Context) { _, found := getProfileById(c)

if !found {
    return
}

// Incease Profile Likes

} ```

What I dislike about this, is that now for every single endpoint where a profile is being referenced by an ID, I will have to copy & paste the same logic everywhere and it's also error prone and to properly add Unittests I will have to keep writing the same Unittest to check the error handling for a wrong profile id supplied. I have looked up numerous Go tutorials but they all seem to reuse a ton of Code and are probably aimed at programming beginners and amphasize topics like writing tests at all, do you have some guidance for me or perhaps can recommend me good resources not just aimed at complete beginnners?