Generate Random Phone Numbers in Go
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Go
What: Generate random phone numbers in Go using math/rand and fmt package. Covers US format, international format, and validation for testing.
Try it: Use our interactive Phone-numbers generator or integrate this code into your Go application.
Generate random phone numbers in Go using math/rand and fmt package. Covers US format, international format, and validation for testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Ruby or use our interactive web generator.
Go Code Example
package main
import (
"fmt"
"math/rand"
"regexp"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Generate random US phone number (xxx) xxx-xxxx
func randomUsPhoneNumber() string {
areaCode := rand.Intn(800) + 200
exchange := rand.Intn(800) + 200
lineNumber := rand.Intn(9000) + 1000
return fmt.Sprintf("(%03d) %03d-%04d", areaCode, exchange, lineNumber)
}
// Generate random phone number with country code
func randomInternationalPhone(countryCode string) string {
areaCode := rand.Intn(800) + 200
exchange := rand.Intn(800) + 200
lineNumber := rand.Intn(9000) + 1000
return fmt.Sprintf("%s (%03d) %03d-%04d", countryCode, areaCode, exchange, lineNumber)
}
// Generate phone number without formatting
func randomPhoneDigits(length int) string {
var sb strings.Builder
for i := 0; i < length; i++ {
sb.WriteString(fmt.Sprintf("%d", rand.Intn(10)))
}
return sb.String()
}
// Validate US phone number format
func isValidUsPhone(phone string) bool {
pattern := `^\(\d{3}\) \d{3}-\d{4}$`
matched, _ := regexp.MatchString(pattern, phone)
return matched
}
// Generate multiple phone numbers
func generatePhoneNumbers(count int) []string {
phones := make([]string, count)
for i := 0; i < count; i++ {
phones[i] = randomUsPhoneNumber()
}
return phones
}
func main() {
fmt.Println("US Phone:", randomUsPhoneNumber())
fmt.Println("International:", randomInternationalPhone("+44"))
fmt.Println("Digits only:", randomPhoneDigits(10))
fmt.Println("Valid:", isValidUsPhone("(555) 123-4567"))
fmt.Println("Batch:", strings.Join(generatePhoneNumbers(3), ", "))
}
[EXPLANATION]
Go uses rand.Intn() for generating area codes (200-999), exchanges (200-999), and line numbers (1000-9999). fmt.Sprintf("%03d") formats with leading zeros. regexp.MatchString() validates patterns. strings.Builder efficiently builds digit-only strings. make([]string, count) pre-allocates slices. strings.Join() concatenates with separators. rand.Seed() in init() ensures different random sequences per execution.
Expected Output
US Phone: (555) 234-5678 International: +44 (555) 234-5678 Digits only: 5551234567 Valid: true Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123
Common Use Cases
- Generate phone numbers for Go microservices testing
- Create contact data for gRPC API testing
- Populate test databases from Go applications
- Test phone validation in web services
- Generate mock data for SMS gateway integration
Important Notes
-
fmt.Sprintf("%03d")formats integers with leading zeros -
rand.Seed()should be called once, typically in init() -
strings.Builderis more efficient than string concatenation -
regexp.MatchString()compiles regex each call; use Compile() for reuse -
Go 1.20+ introduces
rand.N()for simpler random generation
Try Our Interactive Generator
Don't want to write code? Use our free web-based Phone-numbers generator with instant results.
TRY PHONE-NUMBERS GENERATOR →Other Programming Languages
View Phone-numbers generation code examples in PHP
View Phone-numbers generation code examples in JavaScript
View Phone-numbers generation code examples in Python
View Phone-numbers generation code examples in Java
View Phone-numbers generation code examples in C#
View Phone-numbers generation code examples in C++
View Phone-numbers generation code examples in Ruby