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
+31.6k Golang : Get local IP and MAC address
+20.6k Nginx + FastCGI + Go Setup.
+8.4k Golang : How to check if input string is a word?
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+16.5k Golang : Get IP addresses of a domain name
+9.3k Golang : Temperatures conversion example
+29.9k Golang : How to get HTTP request header information?
+9.2k Golang : Create and shuffle deck of cards example
+7.7k Golang : Generate human readable password
+10.6k Golang : How to delete element(data) from map ?
+6.2k Golang : Calculate US Dollar Index (DXY)
+22.7k Golang : Set and Get HTTP request headers example