String Data Type in GoLang

String Data Type:

A string is a combination of two or more than two characters (letters, numbers, or symbols) in a program. The string is written in between pair of backquotes or double quotes in the go language. For example:

"My name is John."

OR

 `My name is John.`
 Both are strings. 

Program:

Here is a program of normal string data type is given below:

 package main
 // program of string data type
 import "fmt"
 func main() {
             var str1 = "Govind"
             var str2 = `Govind`                // string variable declared.
             fmt.Println(str1)
             fmt.Println(str2)
             fmt.Printf(" \n %T \n %T", str1, str2)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

String Data Type in GoLang

Categories of string data type:

If you follow the characteristics of the string, then it can be divided into two categories. These categories are:

  1. Raw string literals
  2. Interpreted string literals

These categories are defined based on the use of quotes.

Raw String Literals:

Raw string literals are those strings that are enclosed by a pair of back quotes or backticks.

You can also say that the characters written in between backticks or back quotes are known as raw string literals.

When you run your go program containing raw string literals, it will display all the characters written in the backquotes except the backticks.

Backslashes have special meaning in the go program. Normally, we use backslashes to represent special characters in the string of a go program.

For example, We use \n in the string to represent the new line, but here in raw string literals, backslashes have no special meaning. Raw string literals will display all characters, even backslashes, in the output as written in between backticks in the program.

Just like \n other escape characters do not have any special meaning in the raw string literals.

There is another important use of raw string literals in the go program. The developer can create a multiple-line string in the program with the help of raw string literals. A raw string has a feature that they can span in multiple lines in the program.

For example:

 `Hey ..!!
 My name is Govind,
 And I love my life.` 

The above string is a multiple-line string. In this multiple line string, the new line is created by the program's user; no special operation is performing here to create a new line.

Program:
 package main
 /* program of string data type by using
 raw string literals. */
 import "fmt"
 func main() {
             var str1 = `hi..!!
             My name is "Govind" \n
             and... I "Love" My Life`
             /* here, string variable (str1) declared in multiple
             line containing one special character i.e. \n */
             fmt.Println(str1)
             fmt.Printf(" %T", str1)
 } 
Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

String Data Type in GoLang

Here in the output, the space of a single tab in the resultant string occurred because of the program's writing standard. It can be resolve by removing the whitespace in the raw string of the program.

Interpreted String Literals:

Interpreted string literals are those enclosed with the pair of double quotes in the go program.

You can also say that the string written in between double quotes in the go program is known as interpreted string literals.

Interpreted string uses double quotes in the program and displays all the characters written inside it except that characters have special meaning and unescaped double-quotes. It means newline characters, i.e., \n and other characters that have special meaning in the program, will work in this string.

If you want to display the double quotes in the interpreted string, you can do this with the backslash's help. Without backslash, it is not possible to display the double quotes in the interpreted string. Here backslash will work as an escape character in the string. For example:

str = "I \ "Love\" My Life…"

In the output, the above string will display like

I "Love" My Life…

Interpreted string literals represent a single-line string in the go program. It literally can't be spread in multiple lines. Even if you do this, the compiler will give the error; the compiler considers the first word of the second line as a variable in the program, and a variable can't end with double quotes, so an error occurs.

Program:

Here is a program of interpreted string literals are given below, which will give you full clarification about this string:

 package main
 /* Program of string data type by using
 interpreted string literals*/
 import "fmt"
 func main() {
             var str1 = "Hi..!! \nMy name is \"Govind\" \nand ... I \"love\" My Life."
             /* here, string variable declared with a special character
             and double quotes by using the backslash.
             you can’t declare this string in the multiple line
             like raw string literals*/
             fmt.Println(str1)
             fmt.Printf(" %T", str1)
 } 
Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

String Data Type in GoLang



ADVERTISEMENT
ADVERTISEMENT