What is strconv.AppendBool() Function in Golang

The strconv.AppendBool() function in Golang is “used to append the boolean values (true or false), based on the value of num2 to num1, and returns the extended buffer”.

Syntax

func AppendBool(dst []byte, b bool) []byte

Parameters

  1. dst: It is a byte array to which the boolean value (true or false) is to be appended.
  2. b: It is a boolean value (true or false) to be appended.

Return value

The AppendBool() function returns the extended buffer after appending the given boolean value.

Example 1: How to Use strconv.AppendBool() Function

package main

import (
  "fmt"
  "strconv"
)

func main() {
 var b []byte

 // Append a boolean value to the byte slice
 b = strconv.AppendBool(b, true)

 fmt.Printf("%q\n", b)
}

Output

"true"

Example 2: Passing the second argument to true

If the boolean value is true, it will append the string “true“. If it is false, it will append the string “false”.

package main

import (
  "fmt"
  "strconv"
)

func main() {
  dst := []byte("The answer is: ")
  dst = strconv.AppendBool(dst, true)
  fmt.Println(string(dst))
}

Output

The answer is: true

That’s it.

Leave a Comment