ADVERTISEMENT
ADVERTISEMENT

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

Parameter(s):

  • 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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.