如何检查字符串中是否有特殊字符或GoLang中的字符是否为特殊字符

After reading a string from the input, I need to check if there is a special character in it

I ended up doing something like this

alphabet := "abcdefghijklmnopqrstuvwxyz"
alphabetSplit := strings.Split(alphabet, "")
inputLetters := strings.Split(input, "")

for index, value := range inputLetters {
        special:=1
        for _, char :=range alphabetSplit{
            if char == value {
                special = 0
                break
            }
        }

It might have anything wrong because since I used it to something specific i had to edit to post it here

Depending on your definition of special character, the simplest solution would probably to do a for range loop on your string (which yield runes instead of bytes), and for each rune check if it is in your list of allowed/forbidden runes.

See Strings, bytes, runes and characters in Go for more about the relations between string, bytes and runes.

Playground example

You can use strings.ContainsAny to see if a rune exists:

package main

import (
  "fmt"
  "strings"
)

func main() {
  fmt.Println(strings.ContainsAny("Hello World", ",|"))
  fmt.Println(strings.ContainsAny("Hello, World", ",|"))
  fmt.Println(strings.ContainsAny("Hello|World", ",|"))
}

Or if you want to check if there are only ASCII characters, you can use strings.IndexFunc:

package main

import (
    "fmt"
    "strings"
)

func main() {
    f := func(r rune) bool {
        return r < 'A' || r > 'z'
    }
    if strings.IndexFunc("HelloWorld", f) != -1 {
        fmt.Println("Found special char")
    }
    if strings.IndexFunc("Hello World", f) != -1 {
        fmt.Println("Found special char")
    }
}

You want to use the unicode package, which has a nice function to check for symbols.

https://golang.org/pkg/unicode/#IsSymbol

package main

import (
    "fmt"
    "unicode"
)

func hasSymbol(str string) bool {
    for _, letter := range str {
        if unicode.IsSymbol(letter) {
            return true
        }
    }
    return false
}

func main() {   
    var strs = []string {
        "A quick brown fox",
        "A+quick_brown<fox",
    }

    for _, str := range strs {
        if hasSymbol(str) { 
            fmt.Printf("String '%v' contains symbols.
", str)
        } else {
            fmt.Printf("String '%v' did not contain symbols.
", str)   
        }
    }
}

This will provide the following output:

String 'A quick brown fox' did not contain symbols.
String 'A+quick_brown<fox' contains symbols.