Last active
October 8, 2015 21:15
-
-
Save caelifer/65d20a19105866fa771a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"fmt" | |
) | |
type User struct { | |
name, pass string | |
} | |
func (u User) Base64() string { | |
return encode64(u.name + ":" + u.pass) | |
} | |
func encode64(s string) string { | |
return base64.StdEncoding.EncodeToString([]byte(s)) | |
} | |
func decode64(s string) (string, error) { | |
data, err := base64.StdEncoding.DecodeString(s) | |
// Check for error | |
if err != nil { | |
return "", err | |
} | |
return string(data), nil | |
} | |
func main() { | |
var s string | |
fmt.Println(User{name: "test_name"}.Base64()) | |
s, _ = decode64("dGVzdF9uYW1lOg==") | |
fmt.Printf("%q\n", s) | |
fmt.Println(User{name: "Aladdin", pass: "open sesame"}.Base64()) | |
s, _ = decode64("QWxhZGRpbjpvcGVuIHNlc2FtZQ==") | |
fmt.Printf("%q\n", s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - http://play.golang.org/p/UD2TUfDgl6