将字符串转换为字符串类型的切片

che*_*ry7 3 go

在下面的代码中,当我检查时,s[i]它给出了二进制数字而不是字符。代码仍然有效,但如何s[i]在仍然使用 s 类型字符串作为参数的同时返回字符?

func main() {
    
        var ip string
        fmt.Println("Enter string:")
        fmt.Scanf("%s\n", &ip)
        ip = strings.ToLower(ip)
        fmt.Println(isP(ip))
    }
    //Function to test if the string entered is a Palindrome
    
    func isP(s string) string {
        mid := len(s) / 2
        last := len(s) - 1
        for i := 0; i < mid; i++ {
            if s[i] != s[last-i] {
                return "NO. It's not a palindrome."
            }
        }
        return "YES! You've entered a palindrome"
    }
Run Code Online (Sandbox Code Playgroud)

Ken*_*ant 8

如果你想转换string为 a[]string你也可以使用strings.Split(s,"")