Golang Program to Check if the String is Alphanumeric
Last Updated :
04 Apr, 2022
In this article, we will see how to validate an alphanumeric string in Golang. We will simply input a string from the user and check if the string is alphanumeric or not. We will be using the regexp module in Go to validate the alphanumeric patterning of the string.
String Input
To get the input from the user, we will be using the Scan function from the fmt module in Golang. We will store the input in a string variable.
Go
// Golang program to take input string from user
package main
import (
"fmt"
)
func main() {
var word string
fmt.Print("Enter any string: ")
fmt.Scan(&word)
}
In the above program, we have initialized a variable of type string. Followed by declaration we have a simple text message for the user input. Finally, we use the Scan function and store the input in the variable.
Checking For Alphanumeric Regex
After we have the input from the user, we can now move towards validating the string as an alphanumeric string. To do that, we will first import the regexp module. After importing the module, we will access to MustCompile and MatchString functions.
Go
// Go program to check Alphanumeric Regex
package main
import (
"fmt"
"regexp"
)
func main() {
var word string
fmt.Print("Enter any string: ")
fmt.Scan(&word)
is_alphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(word)
}
Using the MustCompiler function, we can check if the regular expression is satisfied or not, We have parsed the string ^[a-zA-Z0-9_]*$ which will check from start(^) to end($) any occurrences of the characters from 0-9, A-Z and a-z. We combine the function with MatchString which will compare the regular expression to the passed string. It will simply return true if the regular expression evaluated is matched with the string else false if the pattern is not matched.
Thus, by using the MustCompile and MatchString functions, we can validate any string to check if it's alphanumeric or not. So, we can further use conditional statements to print the message accordingly.
Go
// Go program to check Alphanumeric Regex
package main
import (
"fmt"
"regexp"
)
func main() {
var word string
fmt.Print("Enter any string: ")
fmt.Scan(&word)
is_alphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(word)
fmt.Print(is_alphanumeric)
if is_alphanumeric{
fmt.Printf("%s is an Alphanumeric string", word)
} else{
fmt.Printf("%s is not an Alphanumeric string", word)
}
}
Output: So, the script is working as expected and giving the appropriate output for different string combinations.

Converting the Script into a function
We can convert the above script into a function for better usage irrespective of the requirement and conditions for the project.
Go
// Go program to check Alphanumeric string
package main
import (
"fmt"
"regexp"
)
func is_alphanum(word string) bool {
return regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(word)
}
func main() {
var word string
fmt.Print("Enter any string: ")
fmt.Scan(&word)
is_alphanumeric := is_alphanum(word)
if is_alphanumeric{
fmt.Printf("%s is an Alphanumeric string", word)
} else{
fmt.Printf("%s is not an Alphanumeric string", word)
}
}
Output:
Similar Reads
How to Get the String in Specified Base in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
2 min read
How to check the specified rune in Golang String? In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In the Go strings, you are allowed to check the given string contain the spec
3 min read
How to Generate Random String/Characters in Golang? We might want to generate random strings or even sets of characters to perform some operations or add certain string-related functionality into an application. We can randomly get a character from a set of characters, randomize the order of characters of a given string or generate a random string. W
6 min read
Golang | Extracting all the Regular Expression from the String A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. I
2 min read
Check If the Rune is a Decimal Digit or not in Golang Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world's writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode c
2 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
Golang | Extracting a Regular Expression from the String A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. I
2 min read
Searching an element of string type in Golang slice In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search
3 min read
How to find the Length of Channel, Pointer, Slice, String and Map in Golang? In Golang, len function is used to find the length of a channel, pointer, slice, string, and map. Channel: In Go language, a channel is a medium through which a goroutine communicates with another goroutine and this communication is lock-free. C // Go program to illustrate // how to find the length
2 min read
Golang | Finding Index of the Regular Expression present in String A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. I
2 min read