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
+29.3k Golang : How to convert(cast) string to IP address?
+4.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+10k Golang : How to use if, eq and print properly in html template
+14.2k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+5.6k Golang : Calculate pivot points for a cross
+9k CodeIgniter : Load different view for mobile devices
+12.6k Golang : How to check if a file is hidden?
+25.5k Golang : How to check if a connection to database is still alive ?
+15k Golang : Get file permission
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+6.8k Javascript : How to check a browser's Do Not Track status?
+9k Golang : Print how to use flag for your application example