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
+9.8k Golang : Find correlation coefficient example
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+8.3k Golang : Tell color name with OpenCV example
+14.1k Golang : How to check if a file is hidden?
+18.6k Golang : Write file with io.WriteString
+7.8k Golang : Convert(cast) io.Reader type to string
+30.4k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+11.2k Golang : Generate random elements without repetition or duplicate
+17.4k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+6k Golang : List all packages and search for certain package
+9.5k Golang : How to get ECDSA curve and parameters data?
+5.2k Golang : Display packages names during compilation