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
+16k Golang : Get digits from integer before and after given position example
+5.5k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+21.2k Golang : Sort and reverse sort a slice of strings
+8.3k Golang : Get final or effective URL with Request.URL example
+17.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+5.4k Golang : How to deal with configuration data?
+21.9k Golang : How to reverse slice or array elements order
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+13.5k Golang : Linear algebra and matrix calculation example
+5.8k List of Golang XML tutorials
+16.8k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+26.2k Mac/Linux and Golang : Fix bind: address already in use error