How to append data to a byte slice using the bytes package in Go?

To append data to a byte slice using the bytes package in Go, you can follow these steps:

  1. Import the bytes package:
import "bytes"
  1. Create a byte slice:
var data []byte
  1. Use the bytes package's Append() function to append data to the byte slice:
data = bytes.Append(data, []byte("Hello, ")) data = bytes.Append(data, []byte("world!"))
  1. Print the byte slice to see the appended data:
fmt.Println(string(data))

The complete code snippet will look like this:

package main import ( "bytes" "fmt" ) func main() { var data []byte data = bytes.Append(data, []byte("Hello, ")) data = bytes.Append(data, []byte("world!")) fmt.Println(string(data)) }

When you run this code, it will print "Hello, world!".