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
+5.5k Golang : How to write backslash in string?
+8.7k Golang : How to get ECDSA curve and parameters data?
+6.9k Golang : Get YouTube playlist
+10.2k Golang : Removes punctuation or defined delimiter from the user's input
+11.1k Golang : Find and draw contours with OpenCV example
+7.5k Golang : Variadic function arguments sanity check example
+41.9k Golang : Get hardware information such as disk, memory and CPU usage
+13.2k Golang : Compress and decompress file with compress/flate example
+8.4k Golang : Populate or initialize struct with values example
+9.9k Generate Random number with math/rand in Go
+8.1k Golang : Set or add headers for many or different handlers
+10.3k Nginx : TLS 1.2 support