×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strconv.AppendBool() Function with Examples

Golang | strconv.AppendBool() Function: Here, we are going to learn about the AppendBool() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 06, 2021

strconv.AppendBool()

The AppendBool() function is an inbuilt function of the strconv package which is used to append the bool values ("true" or "false"), according to the value of b, to dst and returns the extended buffer. Where dst is the first parameter of []byte type and b is the second parameter of bool type.

It accepts two parameters (dst, b) and returns the extended buffer.

Syntax

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

Parameters

  • dst : A byte array (or byte slices) in which we have to append the bool value (true or false).
  • b : A bool value (true or false) to be appended.

Return Value

The return type of AppendBool() function is a []byte, it returns the extended buffer after appending the given bool value.

Example 1

// Golang program to demonstrate the
// example of strconv.AppendBool() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x := []byte("Bool:")
	fmt.Println("Before AppendBool()")
	fmt.Println(string(x))

	// Appending 'true' to the existing value of x
	x = strconv.AppendBool(x, true)
	fmt.Println("After AppendBool()")
	fmt.Println(string(x))
}

Output:

Before AppendBool()
Bool:
After AppendBool()
Bool:true

Example 2

// Golang program to demonstrate the
// example of strconv.AppendBool() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x := []byte("Bool:")
	fmt.Println("Before appending...")
	fmt.Println("x:", string(x))
	fmt.Println("Length(x): ", len(x))

	// Appending 'true' and then 'false'
	x = strconv.AppendBool(x, true)
	x = strconv.AppendBool(x, false)

	fmt.Println("After appending...")
	fmt.Println("x:", string(x))
	fmt.Println("Length(x): ", len(x))
}

Output:

Before appending...
x: Bool:
Length(x):  5
After appending...
x: Bool:truefalse
Length(x):  14

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.