Feed Go programs with external data

Hello, I want to create one function for two purposes: Write data into either one variable or into a slice. Data means it’s some FFI like situation, like a database or hardware that gives me the data so that I need to import “unsafe” and use these kind of functions.

The data source is just an uintptr pointer that points to the external data.

Foo(&v) must write one element into v.
Foo(&s) or Foo(s) must write len(s) elements into the slice s.

The point is to use one function so that nobody uses Foo to write 12 bytes of input data into a slice header by accident, because Foo(&v) would always exist, and it will discard the type (safety) in order to write the data.

So, I could look at binary.Write and do what already has been done. But that package gave me so many problems until today. Naturally it can’t handle integers of platform dependant sizes until I add more code, it annoyed me. And today it failed to work with a simple float32. I don’t get it anymore. Normally one learns how to use something, but I forget how that thing works at all.

Have you any idea how to bring external data into single variables and slices and make it be a good programming interface? I mean, I don’t really think the binary module is good. I simply don’t get what it does when it fails - too much stuff in the backtrace.

Anything will do, just any example except that way “binary” does it, which fails at runtime for a number of reasons, apparently.

Thank you a lot!

That was me, I’m Tomas, I’m now using my noob account again. I think I might not have explained it well. There’s a few components to the problem, I think about a bunch of things. Some of them might be because Go is used in specific areas (server etc., and I try to make desktop software with it), some not.

  1. One is, might it be overwhelming to go a standard route in Go? I mean, I have not ever tried Gob, Protobuf and other topics in and around the topic of serialization of data, and communication protocols or what ever it actually is. I have some understanding of how to write low level stuff, but not really modern stuff like this.

  2. The problem that I vaguely mentioned was, that I’m new to being given run-time errors when trying to serialize and save to disk a structure. The stuff I have known is C/C++ and crashes for whatever reason, at run-time also. It’s different. Actually in Go, it felt unnecessary to fail at run-time, but I’m sure there are good reasons.

  3. I have no clue what the Go language specs really mean when saying a field alignment is “at least 1”, where they go briefly into detail about struct memory layout. What I have read from their short description is, that I can find out the default alignment for a struct by searching for the highest alignment, but I’m new to that part in Go and have no Idea. In C I just relied on the fact that alignment more or less only changes when I pragma in the packing for a struct, or 32 vs 64 bit. But I did not dive deeper into Go.

  4. When component A wants packing A’, and my favourite language construct in my chosen programming language leads me to packing B, will I regret that might have given the language the most control over the packing when it comes to time critical parts? E.g. I might end up repacking an array of floats in a situation where my graphics card wants these and my PC is already struggling to render my game. Is that a realistic situation where one would like to revert to C++?

  5. In some way saving and loading things might get more granular in Go. One might have to, for example, save two float32 values (x, y) with two lines of code where in another language you’d rather pass the structure in one line. Can Go make you type code where you wished it would behave like another language? Typing code can be quite some work.

That’s my worries with Go vs. C and C++. Also, I’m bad at projects with two and more languages in it. The frictions. Oh, and, I’m not super convinced by the typeparams as it works out in Go, somehow. The Generics plus the somewhat older RTTI got too complex for my simplistic style, which Go actually supported most of the time.

Best regards, thanks for reading.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.