Golang Single, Double, and Reverse Quotes

Article Directory

Preface

I'm learning cool Golang, check out this post Summary of Golang's Learning Notes.

1 Overview

Golang qualifier or string has three types of quotation marks, single quotation mark ('), double quotation mark ('), and back quotation mark (```).Reverse quotation marks are the keys below the Esc button on the standard keyboard.

The corresponding English is: Single quote, Double quote, Back quote.

  • Single quotation mark indicating byte or Rune type, corresponding to uint8 and int32, defaulting to Rune type.Byte is used to emphasize that the data is raw data, not numbers; rune is used to represent Unicode code code point s.

  • Double quotation marks are strings, they are actually arrays of characters.You can access a byte with an index number, or you can use the len() function to get the byte length of a string.

  • Reverse quotation marks, representing the literal amount of a string, do not support any escape sequences.The literal raw literal string means that whatever you define is what it is. If you have a line break, it will be.When you write an escape character, it also displays the escape character.

2 examples

Refer to https://golangbyexample.com/double-single-back-quotes-go/

package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    //String in double quotes
    x := "tit\nfor\ttat"
    fmt.Println("Priting String in Double Quotes:")
    fmt.Printf("x is: %s\n", x)
    
   //String in back quotes
    y := `tit\nfor\ttat`
    fmt.Println("\nPriting String in Back Quotes:")
    fmt.Printf("y is: %s\n", y)
   
    //Declaring a byte with single quotes
    var b byte = 'a'
    fmt.Println("\nPriting Byte:")
    //Print Size, Type and Character
    fmt.Printf("Size: %d\nType: %s\nCharacter: %c\n", unsafe.Sizeof(b), reflect.TypeOf(b), b)
    
    //Declaring a rune with single quotes
    r := '£'
    fmt.Println("\nPriting Rune:")
    //Print Size, Type, CodePoint and Character
    fmt.Printf("Size: %d\nType: %s\nUnicode CodePoint: %U\nCharacter: %c\n", unsafe.Sizeof(r), reflect.TypeOf(r), r, r)
    //Below will raise a compiler error - invalid character literal (more than one character)
    //r = 'ab'
}

Output:

Priting String in Double Quotes:
x is: tit
for	tat

Priting String in Back Quotes:
y is: tit\nfor\ttat

Priting Byte:
Size: 1
Type: uint8
Character: a

Priting Rune:
Size: 4
Type: int32
Unicode CodePoint: U+00A3
Character: £

3 Reverse quotation scenarios for displaying literal quantity

package main

import (
	"log"
)

const (
	doubleQuote string = "\nmain {\nconsole.log(event)\nreturn ret\n};\n"
	backQuote string = `
	main {
		console.log(event)
	};
	`
)

func main() {
	log.Printf("doubleQuote:%v\n", doubleQuote[1])
	log.Printf("backQuote:%s\n", backQuote)
}

Output:

doubleQuote:109
backQuote:
	main {
		console.log(event)
	};

4 Summary

Reverse quotation marks are useful in situations where you need to show the literal amount of a string, such as when you want to show a multiline function.

Single quotation marks are usually used to denote the rune type, showing unicode.

END

245 original articles published, 239 praised, 810,000 visits+
His message board follow

Posted on Thu, 05 Mar 2020 17:06:41 -0800 by adv