How to properly check a string for emptiness in Go: best practices
Hello everyone! I just started learning Go and encountered what seems like a simple task — checking a string for emptiness. But the more I read, the more confused I get.
In Python I was used to just writing `if not my_string:`, and it worked. In Go I see several options:
1. `if len(str) == 0 { ... }`
2. `if str == "" { ... }`
3. Using `len(str) == 0`
Which of these methods is correct? Is there a difference in performance? Maybe there is a built-in function like `IsEmpty()`?
Here's an example of my code where I check if the user entered a name:
```go
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your name: ")
name, _ := reader.ReadString('\n')
name = strings.Trim
```
No answers yet.
Log in to leave an answer