Using IOBuffer with append

I’m reading the documentation on 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.

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)))

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

3 Likes

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:

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 :slight_smile: )

1 Like

how rude of me

1 Like

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

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

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

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?

I see, no, this time :slight_smile: , 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)

1 Like

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.

2 Likes

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

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:

I'm excited to learn Julianization. It has many members.
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:

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

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

3 Likes

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

1 Like

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?

I am still trying and yet I am not there.

It is written literally in the docs: I/O and Network · The Julia Language
And by chance I knew what a keyword argument is. Probably because I have used it before.

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 :slight_smile:
(…yes, yes, I know, best language depends… blabla…)

1 Like