Updated May 24, 2023
Introduction to Golang Buffer
The following article provides an outline for Golang Buffer. In the Go language, the buffer belongs to the byte package of the Go language, and we can use these packages to manipulate the byte of the string. For example, suppose we have a string. We can read the length of the string with the len function, which will return the numeric length, but what if the strings are too large? We want to calculate in the form of chunks of data, so in such situations, we can use the buffer; the buffer allows us to handle any size of the dynamic length, making it flexible.
Syntax
Given below is a simple syntax for the go language byte buffer; we can see in the example, hereafter the creation of the variable strBuffer from the bytes package, we can call its many functions for further calculations; for example, we can do calculations for the length of the string in any format, getting string data from the os stored string and many more functional features we can perform with the variable strBuffer. We can fetch the same from the string variables once we store the value in a buffer.
str Buffer bytes.Buffer
How does a Buffer work in the Go language?
The buffer name itself clarifies its purposes; it allows us to give buffer storage where we can store some data and access the same data when needed. In the case of the string being a large size, there are too many strings that we will store into a variable; then we will create a buffer variable and keep storing the data onto that variable.
We will see the working of the buffer in go language with the help of the below points.
- To use the buffer in the go language, we need to import the bytes package of the go language.
- Once we have imported the bytes package, we can create a variable with the byte package like var x =bytes. Buffer, and on the variable x, we can perform all the operations related to the buffering of string.
- We can store data of string onto the buffer variable x like x.WriteString(“string of message ”), and the data which we stored on the string can be accessed like x.String().
- We can check the length of the string stored on the buffer variable which we have created.
- Even we can store the bytes of data like x.Write([]byte(“Hello “)), and we can get the length of the stored value in the form of a number like x.len().
Examples of Golang Buffer
We are calculating the variable length and variable details stored onto the buffer string, fetching the string, and getting the length of the string with the help of the various methods available on the go language. In case we want to see the output of the below examples, then we can create a file with any name; we have created a file with a name buffer.go and copy and paste the below examples on the file. We can run the command go run buffer.go, and we can see the output of the execution.
Example #1
Code:
package main
import (
"fmt"
)
//importing the bytes package so that buffer can be used
import (
"bytes"
)
func main() {
//Creating buffer variable to hold and manage the string data
var strBuffer bytes.Buffer
strBuffer.WriteString("Ranjan")
strBuffer.WriteString("Kumar")
fmt.Println("The string buffer output is",strBuffer.String())
}
Output:
Example #2
Code:
package main
import (
"fmt"
)
//importing the bytes package so that buffer can be used
import (
"bytes"
)
func main() {
//Creating buffer variable to hold and manage the string data
var strBuffer bytes.Buffer
fmt.Fprintf(&strBuffer, "It is number: %d, This is a string: %v\n", 10, "Bridge")
strBuffer.WriteString("[DONE]")
fmt.Println("The string buffer output is",strBuffer.String())
}
Output:
Example #3
Code:
package main
import (
"fmt"
)
//importing the bytes and os package so that buffer can be used on the os based
import (
"bytes"
"os"
)
func main() {
//Creating buffer variable to hold and manage the string data
var byteString bytes.Buffer
byteString.Write([]byte("Hello "))
fmt.Fprintf(&byteString, "Hello friends how are you")
byteString.WriteTo(os.Stdout)
}
Output:
Example #4
Code:
package main
import (
"fmt"
)
//importing the bytes package so that buffer can be used
import (
"bytes"
)
func main() {
//Creating buffer variable to hold and manage the string data
var strByyte bytes.Buffer
strByyte.Grow(64)
strByytestrByyte := strByyte.Bytes()
strByyte.Write([]byte("It is a 64 byte"))
fmt.Printf("%b", strByytestrByyte[:strByyte.Len()])
}
Output:
Example #5
Code:
package main
import (
"fmt"
)
//importing the bytes package so that buffer can be used
import (
"bytes"
)
func main() {
//Creating buffer variable to hold and manage the string data
var strByyte bytes.Buffer
strByyte.Grow(64)
strByyte.Write([]byte("kumar"))
fmt.Printf("%b", strByyte.Len())
}
Output:
Example #6
Code:
package main
import (
"fmt"
)
//importing the bytes package so that buffer can be used
import (
"bytes"
)
func main() {
//Creating buffer variable to hold and manage the string data
var strByyte = []byte("Ranjan, Kumar")
strByyte = bytes.TrimPrefix(strByyte, []byte("Hello, "))
strByyte = bytes.TrimPrefix(strByyte, []byte("Good we will meet again,"))
fmt.Printf("Hello%s", strByyte)
}
Output:
Conclusion
From this article, we saw the basic concept of the go language buffer, and we saw the syntax of the buffer. We saw the working concept of the buffer, and we also focused on some of the important examples of the buffer, which revealed the buffer’s depth details.
Recommended Articles
We hope that this EDUCBA information on “Golang Buffer” was beneficial to you. You can view EDUCBA’s recommended articles for more information.