Golang

2230 readers
1 users here now

This is a community dedicated to the go programming language.

Useful Links:

Rules:

founded 2 years ago
MODERATORS
1
 
 

I have a situation where generics would be useful: a server (that I do not control or influence) with many API endpoints that each returns very similar json. There's an envelope with common attributes and then an embedded named substructure (the name differs in the return value of each call) of a different type.

Without generics, you could do something like:

type Base struct {
   // common fields
}

type A {
   Base
   A struct {
      // subtype fields
   }
}

type B {
   Base
   B struct {
      // subtype fields
   }
}

but then you'd need to either duplicate a bunch of API calling and unmarshalling code, or consolidate it and do a bunch of type casting and checking.

Generics to the rescue: subtypes become specific types for a general type:

type Base[T any] {
   // common fields
   Subfield T
}

type A struct {
  // subtype fields
}

type B struct {
  // subtype fields
}

It even looks cleaner! Ah, but the rub is that the marshaled field name Subfield is the same for every type: there's no way to specify a tag for a struct type so that Subfield is un/marshaled with a name specific to the type.

https://go.dev/play/p/3ciyUITYZHk

The only thing I can think of is to create a custom unmarshaller for Base and use introspection to handle the specific type.

Am I missing a less hacky (introspection is always hacky) way to set a default tag for any field of a given struct type? How would you do this?

This pattern - APIs using envelopes for data packets - is exceedingly common. I can't believe the only way to solve it on Go is by either mass code duplication, or introspection.

2
3
 
 

cross-posted from: https://lemmy.world/post/22553148

I've been working really hard on implementing a GUI Config Editor for my Mini Macro Pad project.

Mini Macro Pad (go-mmp) is a tool for creating and running macros, shortcuts, and other automated actions at the press of a button.

It works with hardware like Arduino-based macro pads or directly through a desktop GUI, making it versatile for various workflows.

4
5
17
Go Turns 15 (go.dev)
submitted 1 month ago by [email protected] to c/golang
6
 
 

I'm fairly new to go and I've recently migrated a in-memory cache from node to go for concurrency improvements, but the memory usage difference between the two are huge. I've tried to read up on the map memory model in go but haven't been able to find a reason for the difference. I can't see that I'm doing anything special, so I'm looking for guidance here.

The documents that are stored are around 8 KB in size as a JSON file. In node the memory usage for 50000 documents stored as objects is 1,5 GB, and for go maps it is 10 GB.

To me, this doesn't seem reasonable but I can't find the source of the difference. Could anyone here give their take on this?

7
8
9
10
11
 
 

In the original proof of concept for ranging over functions, iter.Pull was implemented via goroutines and channels, which has a massive overhead.

When I dug in to see what the released code did I was delighted to see that the go devs implemented actual coroutines to power it. Which is one of the only ways to get sensible performance from this.

Will the coro package be exposed as public API in the future? Here's to hoping ♥️

12
13
 
 

Always choose the right tool for the job? Nah. I use Go basically everywhere, which either makes me insightful or stupid. Decide for yourself! :D

14
 
 

On my work I'm always switching between databases and kind of tired on UX differences between psql, mysql, sqlite3. So, I'm making a small set of tools for myself in tries to solve that. It's kinda works for me already and I'd like to share it here :)

15
15
submitted 4 months ago by mac to c/golang
16
7
submitted 5 months ago by mac to c/golang
17
24
submitted 5 months ago by mac to c/golang
18
 
 

I made some Go scripts that require user input fmt.Scanln(&fileName) during the execution. When I use the Go debugger built into VSCode which is the launch type, it works but there is no way to enter any prompts when your exeuctable asks for a input. With other programming languages like NodeJS and PHP, there is way to run the scripts in "debugging mode" where it will run the code but before it executes the code, it will wait to attach to a debugger on your system and then execute the code. This has always allowed me to use the terminal for inputs in the executable.

For example to do this in NodeJS, you will use node --inspect-brk=0.0.0.0 main.js instead of node main.js and then run the debugger in VSCode to attach it to the executing script. Is there a way to do this with Go? Do I need to set something up to achieve this?

I am on Linux Mint and cannot find any commands to run go run . but to wait for a debugger to attach to the executable before executing.

19
 
 

From @palkan: Learn how to temporarily stream verbose logs from your @golang applications without restarts and even without changing the application-level logging settings.

https://evilmartians.com/chronicles/realtime-diagnostic-logging-or-how-to-really-spy-on-your-go-web-apps

20
8
submitted 6 months ago by zinderic to c/golang
21
17
Sqlc: 2024 check in (brandur.org)
submitted 6 months ago by jamietanna to c/golang
22
23
24
 
 

cross-posted from: https://discuss.online/post/8349829

Hello,

We're looking for GoLang contributors to help with the federation service. We're happy to have people that contribute code or help with code reviews.

If you know anyone or if you're interested please reach out!

Email: [email protected] Mastodon: https://utter.online/@sublinks Online form: https://sublinks.org/join_organization.html

Thanks, jgrim

25
5
submitted 7 months ago by adhd42 to c/golang
view more: next ›