What is the default value of a string in Go language?

In this tutorial, we are going to learn about the string variable and the default value of a string variable in the Go language.
Submitted by IncludeHelp, on October 02, 2021

The string is an inbuilt data type in Go language. The default value of a string variable is an empty string.

Example 1:

// Golang program to demonstrate the
// default value of a string variable

package main

import "fmt"

func main() {
	var message string

	fmt.Printf("Default value is %q\n", message)
}

Output:

Default value is ""

Example 2:

// Golang program to demonstrate the
// default value of a string variable

package main

import "fmt"

func main() {
	var message string

	// Checking whether variable is
	// an empty string or not

	// Method 1: Checking length
	if len(message) == 0 {
		fmt.Println("Yes, string is an empty string")
	} else {
		fmt.Println("No, string is not an empty string")
	}

	// Method 2: Comparing with an empty string
	if message == "" {
		fmt.Println("Yes, string is an empty string")
	} else {
		fmt.Println("No, string is not an empty string")
	}
}

Output:

Yes, string is an empty string
Yes, string is an empty string

Golang FAQ »



Comments and Discussions!

Load comments ↻





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