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
+14.4k Golang : Chunk split or divide a string into smaller chunk example
+13.5k Golang : Date and Time formatting
+7.6k Golang : Example of custom handler for Gorilla's Path usage.
+20.4k Golang : How to get struct tag and use field name to retrieve data?
+10.3k Golang : Get login name from environment and prompt for password
+32k Golang : How to convert(cast) string to IP address?
+24k Find and replace a character in a string in Go
+11.5k Golang : Intercept and process UNIX signals example
+5.4k PHP : See installed compiled-in-modules
+13.9k Golang : Get user input until a command or receive a word to stop
+7.8k Golang : Test if an input is an Armstrong number example
+6.3k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)