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
+11k Golang : How to pass map to html template and access the map's elements
+17k Golang : Archive directory with tar and gzip
+10.5k Golang : convert(cast) string to integer value
+11.7k Golang : Google Drive API upload and rename example
+14.1k Golang : How to tell if a file is compressed either gzip or zip ?
+6.8k Golang : Handle sub domain with Gin
+15.2k Golang : How to save log messages to file?
+16.1k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+15.1k Golang : Find smallest number in array
+5.8k Golang : Detect sample rate, channels or latency with PortAudio
+12.1k Golang : How to filter a map's elements for faster lookup
+5.6k Nginx : How to block user agent ?