How to generate a pair of RSA private and public key in Go lang? Go lang has a rich set of standard libraries. Using Go’s standard libraries, we can generate RSA private and Public keys. The Crypto standard libraries in Go lang Go lang standard libraries has a rich set of cryptography functions. Here are
Read more
Tag: Go Lang
How to Install Hyperledger Fabric 2.0 in Ubuntu 18.04
Posted onHyperledger Fabric is a consortium blockchain system. It’s performance is relatively good and its modular architecture enables it to be usable in many scenarios. Hyperledger Fabric itself has rich documents and samples of test networks. For beginners, deploying a new network for trying and testing still consumes quite some time. In this post, we will
Read more
How to Install Go 1.13.x on Ubuntu 18.04
Posted onIn Ubuntu 18.04 LTS, the default Go lang version is 1.10. For compatibility reason, the Ubuntu LTS will usually keep the major release version numbers for packages. However, many applications, such as Hyperledger Fabric 2.0, require a newer version of Go. In this post, let’s take a look at how to install a system level
Read more
How to test a file or directory exists in Go?
Posted onHow to test a path (a file or directory exists) in golang? In Bash, the [ -e ] tests can test whether a file or a directory exist. What are the corresponding Go ways for the test? You can use the `os.Stat()` and `os.IsNotExist()` standard libraries functions together to test whether a file or a
Read more
How to send a POST request in Go?
Posted onHow to send a POST request in Go? For example, send a POST of content like ‘id=8’ to a URL like https://example.com/api. In Go, the http package provides many common functions for GET/POST. Related to POST requests, the package provides 2 APIs: Post Post issues a POST to the specified URL. func (c *Client) Post(url,
Read more
How to print a line to STDERR and STDOUT in Go?
Posted onIn Go, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Go, you can use the ‘Println()’ func from the ‘fmt’ package to print a line to STDOUT: import(“fmt”) fmt.Println(“my msg here”) In Go, os.Stderr
Read more
How to get the running process’ parent process’ ID in Go?
Posted onHow to get the running process’ parent process’ ID in Go? In Go, you may call the os.Getppid() func from the os package to get the parent process’ pid. func Getppid() int Getppid returns the process id of the caller’s parent. One example is as follows. $ gore gore version 0.2.6 :help for help gore>
Read more
How to get the running process’ pid in Go?
Posted onHow to get the running process’ pid in Go lang? In Go, you may call the os.Getpid() func from the os package to get the parent process’ pid. func Getpid() int Getppid returns the process id of the caller. One example is as follows. $ gore gore version 0.2.6 :help for help gore> :import “fmt”
Read more
How to get the hostname of the node in Go?
Posted onIn Go lang, how to get the hostname of the node? In Go, you can use the os.Hostname() function to get the hostname of the node. func Hostname() (name string, err error) Hostname returns the host name reported by the kernel. One example is as follows. The main.go source code: package main import ( “fmt”
Read more
How to get the epoch timestamp in Go lang?
Posted onIn Go lang, how to get the epoch timestamp, the number of seconds passed since the epoch? In Go, you can use the time.Now() func to get current time and its Unix() func to convert it into an epoch timestamp: import(“time”) func getEpochTime() int64 { return time.Now().Unix() } If you would convert a time string
Read more
Any good Go REPL implementation / tool?
Posted onGo lang is fast to compile and can be used as a script by go run prog.go. However, a REPL is more convenient for testing/trying some small pieces of code. Any good Go REPL implementation / tool? Try gore https://github.com/motemen/gore Get gore $ go get -u github.com/motemen/gore An example $ gore gore version 0.2.6 :help
Read more
How to get an environment variable in Go?
Posted onIn Go lang, how to get an environment variable? To get an environment variable (e.g. “VAR”) in GO: import “os” import “fmt” fmt.Println(“VAR:”, os.Getenv(“VAR”))
How to convert a byte[] to io.Reader in Go?
Posted onHow to convert a byte[] to io.Reader in Go directly? You can use the NewReader() func from the bytes package of Go to convert bytes to io.Reader. For example, reader := bytes.NewReader(thebytes)
How to read the whole file content into a string in Go?
Posted onHow to read the whole file content into a string in Go? You can use the ReadFile() func from the io/ioutil package in Go to read the whole file content: func ReadFile(filename string) ([]byte, error) For example, read file filepath content and check the error: if bytes, err := ioutil.ReadFile(filepath); err != nil { log.Fatal(“Failed
Read more
In Golang, how to convert a string to unicode rune array and back?
Posted onIn Golang, how to convert a string to unicode rune array and do the reverse way? Convert string s to a rune array: runes := []rune(s) Convert a rune array runes to string: str := string(runes)
In Golang, how to print string to STDERR?
Posted onIn Golang, the fmt.Println() is convenient to print to STDOUT. But how to print string to STDERR? In Go os package, “Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.” So, you can use the WriteString function on File on os.Stderr to print string to
Read more
Converting Error to String in Go Lang
Posted onIn Golang, fmt.Println(err) can print out the error err. But how to convert an error to a string explicitely? In Go, the error type is an interface type that has an interface type error interface { Error() string } An error variable represents any value that can describe itself as a string. Ref: https://golang.org/pkg/builtin/#error So,
Read more
How to process a file line by line in Go?
Posted onIn the Go programming language, How to process a file line by line? An equivalent piece of Bash code could be while read line ; do echo $line done < ./input.txt You can make use of the bufio package’s Scan(). // open the file filepath f := os.Open(filepath) // create a scanner fs := bufio.NewScanner(f)
Read more
How to create a Reader from a string in Go?
Posted onIn Go, how to create a Reader from a string? Many APIs use a Reader as its input. In Go, the strings.NewReader(s) library function creates a Reader from a string. Check the doc at: https://golang.org/pkg/strings/#NewReader