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
+8k Golang : How to check if your program is running in a terminal
+3.7k PHP : Get client IP address
+3k HTTP common errors and their meaning explained
+2.4k Java : Generate multiplication table example
+35k Golang : How to iterate over a []string(array)
+8.5k Golang : Exit, terminating or aborting a program
+4.2k Golang : Muxing with Martini example
+8.7k Golang : Handle or parse date string with Z suffix(RFC3339) example
+3.2k Golang : Join lines with certain suffix symbol example
+10.2k Golang : syscall.Socket example
+15.3k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+12.8k Golang : How to generate QR codes?