# Using IOBuffer with append

**URL:** <https://discourse.julialang.org/t/using-iobuffer-with-append/47010>\
**Category:** New to Julia\
**Created:** [September 21, 2020, 2:11pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010 "2020-09-21T14:11:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:11pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/1 "2020-09-21T14:11:47Z")

</div>

I’m reading the documentation on [IOBuffer](https://docs.julialang.org/en/v1/base/io-network/#Base.IOBuffer), but I can’t seem to get it to function with `append`. I know I might be making a foolish mistake but I can’t seem to find it.

```julia
io = IOBuffer();
write(io, "JuliaLang is a GitHub organization.", " It has many members.")
append(io, "I'm excited to learn Julia") # This part causes an error.
println(String(take!(io)))

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 21, 2020, 2:14pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/2 "2020-09-21T14:14:44Z")

</div>

dont’ you just keep writing to it using `write` instead of `append`?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2020, 2:14pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/3 "2020-09-21T14:14:46Z")

</div>

I believe (didn’t checked) that you just write into the Buffer. Subsequent writes are per se appended. Of course until you close the IOBuffer:

```julia
io = IOBuffer();
write(io, "JuliaLang is a GitHub organization.", " It has many members.")
write(io, "I'm excited to learn Julia") # This part should not cause an error now
println(String(take!(io)))
close(io)

```

(does this work?)  
(Ninjad 🙂 )

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 21, 2020, 2:15pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/4 "2020-09-21T14:15:44Z")

</div>

> [@oheil](#):
>
> (Ninjad 🙂 )

how rude of me

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:16pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/5 "2020-09-21T14:16:09Z")

</div>

I could, but I wanted to try out `append`. This is just for learning, no real reason why i have to use `append`.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:18pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/6 "2020-09-21T14:18:01Z")

</div>

Why doesn’t it work with `append`? This is just out of curiosity, I could just as easily use write as both answers suggest.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2020, 2:19pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/7 "2020-09-21T14:19:08Z")

</div>

`append` isn’t defined as function on an `IOBuffer`  
You can easily write it on your own 🙂

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:21pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/8 "2020-09-21T14:21:03Z")

</div>

That’s what the error message suggested, but in the documentation it states append can be used.

It may take optional keyword arguments:

- `read` , `write` , `append` : restricts operations to the buffer; see `open` for details.

Is the documentation incorrect?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2020, 2:26pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/9 "2020-09-21T14:26:52Z")

</div>

I see, no, this time 🙂 , the documenation is correct.  
But keyword arguments look like this:  
`open("someFile.txt",append=true)`  
which means don’t overwrite an existing file but append whatever comes.  
So, another point you see already:  
`open`  
in this example is not  
`IOBuffer()`.  
How this applies to IOBuffer depends on what you are doing. In your example above you create a new IOBuffer where the keyword append doesn’t make sense.

Here  
`io=IOBuffer()`  
is in result just the same as  
`io=IOBuffer(append=true)`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2020, 2:27pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/10 "2020-09-21T14:27:59Z")

</div>

That’s just a keyword, has nothing to do with the function named `append!`.

Generally `append!` is defined for ordered containers (eg a `Vector`). `IOBuffer` isn’t really meant to be used as a container, just a sink.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2020, 2:34pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/12 "2020-09-21T14:34:34Z")

</div>

No. append is false by default.  
Here is an example to show the difference:

```julia
io = IOBuffer();
write(io, "JuliaLang is a GitHub organization.", " It has many members.");
seekstart(io);
write(io, "I'm excited to learn Julia");
println(String(take!(io)));
close(io);

```

Output:

```julia
I'm excited to learn Julianization. It has many members.

```

```julia
io = IOBuffer(append=true);
write(io, "JuliaLang is a GitHub organization.", " It has many members.");
seekstart(io);
write(io, "I'm excited to learn Julia");
println(String(take!(io)));
close(io);

```

Output:

```julia
JuliaLang is a GitHub organization. It has many members.I'm excited to learn Julia

```

The `seekstart()` has no effect if `append=true`.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:40pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/13 "2020-09-21T14:40:14Z")

</div>

Thanks for the help. Lots of stuff to read and learn!

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 4:18pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/14 "2020-09-21T16:18:03Z")

</div>

Just out of curiosity, how did you develop a deep knowledge of Julia? See how you were able to point out `append` wasn’t a method implemented for `IOBuffer` and that it was `false` by default, how do you learn those things?

For me anyways, the documentation, although proven right, wasn’t entirely clear to me that `append` wasn’t a method for `IOBuffer`.

Was it just exploring the code, and trying things out for yourself?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2020, 4:45pm UTC](https://discourse.julialang.org/t/using-iobuffer-with-append/47010/15 "2020-09-21T16:45:08Z")

</div>

> [@anon60034542](#):
>
> Just out of curiosity, how did you develop a deep knowledge of Julia?

I am still trying and yet I am not there.

> [@anon60034542](#):
>
> See how you were able to point out `append` wasn’t a method implemented for `IOBuffer` and that it was `false` by default, how do you learn those things?

It is written literally in the docs: [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1.6-dev/base/io-network/#Base.open)  
And by chance I knew what a keyword argument is. Probably because I have used it before.

> [@anon60034542](#):
>
> Was it just exploring the code, and trying things out for yourself?

In general both.  
First step: I have a real world problem and try to solve it with Julia. At the beginning I found a solution fast, but reading here and there, I found out, that there are better ways to do it, that I don’t understand at first. So I searched existing codes and tried to improve my code until I understood (more or less) what’s going on.

My advantage is, that I have always a lot of fun using Julia, which makes it easy to learn. It is not an advantage to you, but an advantage to other things I have to learn (like spoken languages some time ago).

And you must know I have decades of experience with multiple other languages, which helps a lot.

If you have fun, don’t give up. It is rewarding. You are learning the best language that currently exits 🙂  
(…yes, yes, I know, best language depends… blabla…)
