# Write to mem or file

**URL:** <https://ziggit.dev/t/write-to-mem-or-file/16468>\
**Category:** Help\
**Tags:** language\
**Created:** [July 2, 2026, 9:32am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468 "2026-07-02T09:32:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [July 2, 2026, 9:32am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/1 "2026-07-02T09:32:30Z")

</div>

I use `writeStruct()` and `takeStruct()` to write / read things to / from a file.  
Is there an (easy) way - without duplicating logical code - to use this mechanism for write to / read from memory?

---

<div class="post-metadata">

**Author:** ![invlpg](https://ziggit.dev/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@invlpg](https://ziggit.dev/u/invlpg)\
**Post date:** [July 2, 2026, 9:33am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/2 "2026-07-02T09:33:56Z")

</div>

`Io.Writer.fixed` and `Io.Reader.fixed`?

---

<div class="post-metadata">

**Author:** ![ScottRedig](https://ziggit.dev/user_avatar/ziggit.dev/scottredig/32/5880_2.png) [@ScottRedig](https://ziggit.dev/u/ScottRedig)\
**Post date:** [July 2, 2026, 9:36am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/3 "2026-07-02T09:36:37Z")

</div>

You probably want `std.Io.Writer.Allocating`, but yeah `std.Io.Reader.fixed`.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [July 2, 2026, 9:48am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/4 "2026-07-02T09:48:46Z")

</div>

Ok sounds good.  
And when ‘choosing’ between file or mem I could point a std.io.Writer or std.io.Reader to the correct implementation?

---

<div class="post-metadata">

**Author:** ![ScottRedig](https://ziggit.dev/user_avatar/ziggit.dev/scottredig/32/5880_2.png) [@ScottRedig](https://ziggit.dev/u/ScottRedig)\
**Post date:** [July 2, 2026, 10:04am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/5 "2026-07-02T10:04:12Z")

</div>

I would expect you to have saving/loading functions which take `*std.Io.Writer`/`*std.Io.Reader`. Then the code which calls those functions creates the actual writer/reader/file. So probably a “save to file”, and a “save to bytes” function; the same for loading. How you actually wire things depends on the larger context of the program, but that’s a first approximation.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [July 2, 2026, 10:09am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/6 "2026-07-02T10:09:16Z")

</div>

Ok thanks! I will manage it with this info.

---

<div class="post-metadata">

**Author:** ![jmctagger](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/8dc957/32.png) [@jmctagger](https://ziggit.dev/u/jmctagger)\
**Post date:** [July 3, 2026, 7:23am UTC](https://ziggit.dev/t/write-to-mem-or-file/16468/7 "2026-07-03T07:23:24Z")

</div>

There’s a little sample [here](https://ziggit.dev/t/file-i-o-basics-0-16/14968#p-63099-writing-11), only for writing, though; the whole top of the doc is file-centric, but I got tired of that and decided on a memory-rather-than-file destination for the Writer demonstration. I think it’s pretty easy to see, from that, how to go both ways. Of interest, perhaps: when the writer is `.fixed(&buf)` (memory destination), there is no need to `.flush()`, of course. It’s a no-op (but may be best left in the code so that when you swap a file in, rather than memory, all should still work).
