Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to stdout without creating a buffer

Tags:

go

Inside a loop I do some calculations and then I want to print a string value from a byte array, once the loop is done print a new line.

Using fmt.Print will allocate a buffer, but all I want to do is print the character to stdout. Is there a way to do that?

for i, i < size; i++ {
    b = a[i] + i * 10
    fmt.Print(string((b)))
}
fmt.Println()
like image 728
RockNinja Avatar asked Jul 13 '26 07:07

RockNinja


1 Answers

You can do this by simply writing to the os.Stdout file:

var buff [1]byte
for i, i < size; i++ {
    b = a[i] + i * 10
    buff[0] = b
    os.Stdout.Write(buff[:])
}
buff[0] = '\n'
os.Stdout.Write(buff[:])

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!