It's a blog

Tag: go

Go Docker SDK, Raw Terminal Ctrl+C handling

I spent my weekend in working on a new project called dockerit. It’s a simple wrapper around Docker written in Go and making use of the Docker SDK.

One of the biggest sticking points for me, being fairly new with the Golang world, was trying to pass stdin stdout and stderr between the container and host terminal correctly, while also having good performance and doing the expected things (like Ctrl+C to cancel).

The full code for setting up and, interacting with and removing my container can be found here. The main steps are broken down below.

Continue reading

Simple Go defer code example

In Go, a defer statement will execute a function call just before the function it is called from returns.

I found that most of the examples of a Go defer call online seemed to do complicated things with numbers. So here is a nice simple example with just text output.

Example

You can run this code yourself in The Go Playground! You can also find a similar example in the Go Tour.

package main import ( "fmt" ) func main() { // This will output first (in order) fmt.Println("One") // This will output last (before this function finishes) defer fmt.Println("Two") // This will be called before the above defer call "Two" secondary() } func secondary() { // This will output after Four (before this function finishes) defer fmt.Println("Three") // This will output (in order) fmt.Println("Four") }
Code language: JavaScript (javascript)
Continue reading

© 2021 Addshore

Theme by Anders NorénUp ↑