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
+11.1k Golang : How to flush a channel before the end of program?
+28.1k Golang : Change a file last modified date and time
+13.6k Golang : convert(cast) string to float value
+21.8k Golang : Securing password with salt
+9.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+29.5k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+14.8k Golang : How do I get the local IP (non-loopback) address ?
+9.8k Golang : Channels and buffered channels examples
+11.3k Golang : Simple file scaning and remove virus example
+12.8k Golang : Convert(cast) uintptr to string example
+27.2k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+11.5k Golang : convert(cast) float to string