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
+5.7k Golang : Find the shortest line of text example
+11.6k Golang : Convert int(year) to time.Time type
+8.6k Golang : Eroding and dilating image with OpenCV example
+4.8k Golang : Struct field tags and what is their purpose?
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+6.6k Golang : Gorrila set route name and get the current route name
+11.3k Golang : Covert map/slice/array to JSON or XML format
+22.2k Golang : Print out struct values in string format
+18.6k Golang : Set or Add HTTP Request Headers
+19.9k Golang : Encrypt and decrypt data with TripleDES
+9.4k Golang : Underscore string example