Golang : How to get username from email address
Just need a simple solution to extract the username from a given email address. The code below uses strings.Split()
function to split an email address in between the @
symbol and we take the position 0
from the resulting slice to get the username
Here you go!
package main
import (
"fmt"
"strings"
)
func main() {
username := strings.Split("iamspammer@gmail.com", "@")
fmt.Printf("%q\n", username[0])
}
Output:
"iamspammer"
Hope this helps and happy coding!
See also : Golang : Chunk split or divide a string into smaller chunk example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.5k Golang : Find the longest line of text example
+4.4k JavaScript : Rounding number to decimal formats to display currency
+6.8k Golang : Levenshtein distance example
+6.8k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+16k Golang : How to extract links from web page ?
+10.8k Golang : Generate random elements without repetition or duplicate
+5.7k Golang : Generate multiplication table from an integer example
+5.8k nginx : force all pages to be SSL
+9.3k Golang : Read file with ioutil
+11.6k Golang : Convert(cast) bigint to string
+33.4k Golang : All update packages with go get command
+9.5k Golang : Format strings to SEO friendly URL example