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