Last active
December 3, 2021 00:44
-
-
Save Mo3g4u/dfe8fef0704fb714569f7f8fcf53faa6 to your computer and use it in GitHub Desktop.
go encode sample
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" | |
"io" | |
"io/ioutil" | |
"log" | |
"strings" | |
) | |
const Message = "44GC44GE44GG44GI44GK" | |
// エンコードのpipe版 | |
// | |
// https://stackoverflow.com/questions/49135097/base64-encode-io-reader | |
func main() { | |
source := strings.NewReader("Hello, World!") | |
pr, pw := io.Pipe() | |
encoder := base64.NewEncoder(base64.StdEncoding, pw) | |
go func() { | |
_, err := io.Copy(encoder, source) | |
encoder.Close() | |
if err != nil { | |
pw.CloseWithError(err) | |
} else { | |
pw.Close() | |
} | |
}() | |
out, err := ioutil.ReadAll(pr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(out)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment