Find numeric phone numbers from words and find vanity numbers from numeric phone numbers! This is a Go library.
gophonewords is a Go library designed to convert between vanity numbers (phone numbers containing letters) and standard numeric phone numbers.
A similar library is also available on pub.dev, for usage in Dart: phonewords package
The gophonewords library provides a straightforward way to transform letter-based phone numbers (like "1-800-FLOWERS") into their numeric equivalents ("1-800-356-9377").
Please note that gophonewords is still a very young library and has some limitations:
- It cannot currently break numeric phone numbers into common chunks (e.g., separating "5551234567" into "555-123-4567").
- It does not convert numeric phone numbers into string-formatted phone numbers (e.g., adding hyphens or parentheses).
-
Create your Go project. Create a new directory for your project and a Go file where you'll use
gophonewords. -
Initialize your Go module. Open your terminal in your project's root directory and run
go mod init <your_module_name>(e.g.,go mod init myapp). This will create ago.modfile. -
Import
gophonewords. In your Go file, add the import statement:import "gophonewords"
Or, if you have other imports:
import ( "fmt" // another package here "gophonewords" )
-
Fetch dependencies. Run
go mod tidyin your terminal. This command will addgophonewordsto yourgo.modfile and download the package.
The core functionality of the gophonewords package is provided by the NumberFromString() function.
package main
import (
"fmt"
"gophonewords" // Assuming gophonewords is in your Go module path
)
func main() {
// Convert a vanity number to a numeric phone number
vanityPhone := "1-800-CALL-NOW"
numericPhone, err := gophonewords.NumberFromString(vanityPhone)
if err != nil {
fmt.Printf("Error converting '%s': %v\n", vanityPhone, err)
return
}
fmt.Printf("'%s' converts to '%s'\n", vanityPhone, numericPhone) // Output: "1-800-CALL-NOW" converts to "1-800-225-5669"
// Example with mixed digits and letters
mixedPhone := "PH0N3W0RD5"
convertedMixed, err := gophonewords.NumberFromString(mixedPhone)
if err != nil {
fmt.Printf("Error converting '%s': %v\n", mixedPhone, err)
return
}
fmt.Printf("'%s' converts to '%s'\n", mixedPhone, convertedMixed) // Output: "PH0N3W0RD5" converts to "7406390735"
// Example with only digits (will return the same digits)
numericOnly := "1234567890"
convertedNumeric, err := gophonewords.NumberFromString(numericOnly)
if err != nil {
fmt.Printf("Error converting '%s': %v\n", numericOnly, err)
return
}
fmt.Printf("'%s' converts to '%s'\n", numericOnly, convertedNumeric) // Output: "1234567890" converts to "1234567890"
// Example with an empty string
emptyString := ""
_, err = gophonewords.NumberFromString(emptyString)
if err != nil {
fmt.Printf("Error converting '%s': %v\n", emptyString, err) // Output: Error converting '': phone number cannot be empty
}
}gophonewords is released under the BSD 3-Clause Revised License. See the LICENSE file for more details.
gophonewords openly welcomes contribution. See the file CONTRIBUTING.md for more information.