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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Use Case Diagram - Unified Modeling Language (UML) A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read
Half Wave Rectifier A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th
15 min read
What is Agile Methodology? The Agile methodology is a proper way of managing the project with breaking them into smaller phases which is iteration. It basically focus on flexibility of the project which we can change and improve the team work regularly as per requirements.Table of ContentWhat is Agile?What is the Agile Method
14 min read
How to Download and Install the Google Play Store The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed?In this step-by-step guide, youâll learn how to safely download and install
6 min read
MVC Framework Introduction Over the last few years, websites have shifted from simple HTML pages with a bit of CSS to incredibly complex applications with thousands of developers working on them at the same time. To work with these complex web applications developers use different design patterns to lay out their projects, to
6 min read
Transaction in DBMS In a Database Management System (DBMS), a transaction is a sequence of operations performed as a single logical unit of work. These operations may involve reading, writing, updating, or deleting data in the database. A transaction is considered complete only if all its operations are successfully ex
10 min read
Best Way to Master Spring Boot â A Complete Roadmap In the corporate world, they say "Java is immortal!". But Why? Java remains one of the major platforms for developing enterprise applications. Enterprise Applications are used by large companies to make money. Those applications have high-reliability requirements and an enormous codebase. According
14 min read
Bash Scripting - If Statement Bash is an interpreter for command languages. It is a default command interpreter on most GNU/Linux systems and is widely available on various operating systems. The name is an abbreviation for Bourne-Again SHell. Scripting enables for the execution of instructions that would otherwise be executed o
15 min read