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
+7.7k Golang : Test a slice of integers for odd and even numbers
+4.8k Golang : Warp text string by number of characters or runes example
+16.7k Golang : Underscore or snake_case to camel case example
+7.3k Golang : Populate or initialize struct with values example
+36.7k Golang : Convert []byte to image
+9k Golang : Add ASCII art to command line application launching process
+9.8k Golang : Convert(cast) float to int
+7.6k Golang : Check if user agent is a robot or crawler example
+5.8k Golang : Get all countries phone codes
+8.8k Golang : Sort and reverse sort a slice of runes
+35k Golang : Remove dashes(or any character) from string
+6.9k Golang : Serving HTTP and Websocket from different ports in a program example