Golang encoding/pem.Decode() function example
package encoding/pem
Decode will find the next PEM formatted block (certificate, private key etc) in the input. It returns that block and the remainder of the input. If no PEM data is found, p (1st return variable) is nil and the whole of the input is returned in rest ( 2nd return variable) .
Golang encoding/pem.Decode() function usage example
// Load PEM
pemfile, err := os.Open("private.pem")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// need to convert pemfile to []byte for decoding
pemfileinfo, _ := pemfile.Stat()
var size int64 = pemfileinfo.Size()
pembytes := make([]byte,size)
// read pemfile content into pembytes
buffer := bufio.NewReader(pemfile)
_, err = buffer.Read(pembytes)
// proper decoding now
data, _ := pem.Decode([]byte(pembytes))
See https://www.socketloop.com/tutorials/golang-tutorial-on-loading-gob-and-pem-files for full example
Reference :
Advertisement
Something interesting
Tutorials
+14k Golang : convert rune to unicode hexadecimal value and back to rune character
+10k Golang : Setting variable value with ldflags
+10.8k Golang : Natural string sorting example
+9k Golang : Build and compile multiple source files
+11.7k Golang : Calculations using complex numbers example
+9.9k Golang : Turn string or text file into slice example
+5.7k Golang : Error handling methods
+7.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+6.1k Golang : How to write backslash in string?
+10.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+19.8k Golang : Append content to a file