Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
communitycreator

How to use the strconv.AppendQuote() function in Golang

Maria Elijah

Golang strconv.AppendQuote()

The strconv package’s AppendQuote() method is used to append the double-quoted Go string literal representing s (as generated by the Quote() function) to dst and return the extended buffer.

Syntax

func AppendQuoteRune(dst []byte, r rune) []byte

Parameters

  • dst: This is a byte array to which the double-quoted string is to be appended.
  • s: This is the double-quoted string to be appended.

Return value

The function AppendQuote() returns the extended buffer after appending the double-quoted string.

Code

The following code shows how to use the AppendQuote() function in Golang.


// Using strconv.AppendQuote() Function 

package main

import (
	"fmt"
	"strconv"
)

func main() {
  // Declaring and assigning value
	x := []byte("Quote 1:")
	fmt.Println()
	fmt.Println("Before AppendQuote():", string(x))

	// Using AppendQuote to appending a quote to the x
	x = strconv.AppendQuote(x, `"Keep learning! keep coding!"`)
	fmt.Println()
	fmt.Println("After  AppendQuote()", string(x))

    // Declaring and assigning value
	y := []byte("Quote 2:")
	fmt.Println()
	fmt.Println("Before AppendQuote():", string(y))

	// Using AppendQuote to appending a quote to the x
	y = strconv.AppendQuote(y, `"Shot on strconv.AppendQuote()"`)
	fmt.Println()
	fmt.Println("After  AppendQuote()", string(y))
}

Code explanation

  • Line 4: We add the main package.

  • Lines 6–9: We import the necessary packages.

  • Lines 11–25: We define the main() function and the variables x and y of byte array type, and assign a value to each.

  • Lines 18-20: We pass the variable to the AppendQuote() function, which appends the double-quoted Go string literal.

  • Finally, the double-quoted string is displayed.

RELATED TAGS

golang
communitycreator
RELATED COURSES

View all Courses

Keep Exploring