by Sai gowtham

How to check if a string is empty in Go

To check if a given string is empty or not, we can use the built-in len() function in Go language.

Here is an example:

package main
import "fmt"

func main() {
    str := "";

    if(len(str) == 0){
       fmt.Println("String is empty")
    }else{
        fmt.Println("String is not empty")
    }
}

Output:

String is empty

or we can use the str == "" to check if a string is empty.

package main
import "fmt"

func main() {
    str := "Hello";

    if(str == ""){
       fmt.Println("String is empty")
    }else{
        fmt.Println("String is not empty")
    }
}

Output:

String is not empty

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course