Golang program to clear the string buffer


When a string buffer is cleared, all of the data that was previously stored inside the buffer is deleted. This can be done for a variety of reasons, including when you want to reuse the buffer for fresh data or when the data that is currently in the buffer is no longer required. Here we will understand different techniques of clear a string buffer using go programming language

Syntax

Reset()

Any accumulated data is discarded and the buffer is reset to zero using the Reset() method. The old buffer is essentially replaced with a new one and the old buffer is left empty when a new buffer variable is created and assigned to the old buffer variable.

Truncate()

By giving 0 as an input, the Truncate(0) method truncates all of the bytes in the buffer, thereby wiping its contents while maintaining the buffer's capacity. The method discards all but the first n unread bytes from the buffer.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and bytes package.

  • Step 2 − Using the internal function to clear the buffer.

  • Step 3 − The output will be printed on the console using fmt.Println() function.

Example1

In this example we will clear the string buffer using reset method which is a built-in method used to clear the strings.

package main
import (
	"bytes"
	"fmt"
)

func main() {
	var buffer bytes.Buffer
	buffer.WriteString("Hello, alexa!")  //Add string to the buffer using writestring
	fmt.Println("The string before using reset method is:")
	fmt.Println(buffer.String()) //print the string

	buffer.Reset() //reset the string and empty it
	fmt.Println("The string after using reset method is:")
	fmt.Println(buffer.String()) //print empty string
}

Output

The string before using reset method is:
Hello, alexa!
The string after using reset method is:

Example 2

In this example, we will see how to clear a string buffer using buffer variable. The output printed will be an empty string printed on the console. Let’s see through the algorithm and the code to see how its execution takes place.

package main
import (
	"bytes"
	"fmt"
)

func main() {
	var buffer bytes.Buffer
	buffer.WriteString("Hello, alexa!")  //add the string to buffer using writestring
	fmt.Println("The string before emptying it is:")
	fmt.Println(buffer.String())   //print the string

	buffer = bytes.Buffer{} //create a new empty buffer
	fmt.Println("The string after emptying it is:")
	fmt.Println(buffer.String())  //print empty string
}

Output

The string before emptying it is:
Hello, alexa!
The string after emptying it is:

Example 3

In this example, we will see how to clear the string buffer using the Truncate() method.

package main
import (
	"bytes"
	"fmt"
)
func main() {
	var buffer bytes.Buffer
	buffer.WriteString("Hello, alexa!")  //add string to the buffer using writestring
	fmt.Println("The string before emptying it is:")
	fmt.Println(buffer.String())  //print string

	buffer.Truncate(0)
	fmt.Println("The string after emptying it is:")
	fmt.Println(buffer.String())  //print empty string
}

Output

The string before emptying it is:
Hello, alexa!
The string after emptying it is:

Conclusion

We executed the program of clearing the string buffer using three examples. In the first example we used reset method, in the second example we created an empty buffer variable and in the third example we used truncate method to execute the program. Hence, program executed successfully.

Updated on: 01-Feb-2023

746 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements