Skip to content

Instantly share code, notes, and snippets.

View fr-xiaoli's full-sized avatar

Xiaoli Shen fr-xiaoli

View GitHub Profile
package main
import "sort"
type Event struct {
pos int
eType int // 1 for start, -1 for end
}
type SortEvent []Event
@fr-xiaoli
fr-xiaoli / Docker-cheatsheet.md
Last active March 27, 2020 07:04
Docker cheatsheet

Images

Build a Docker image and tag it using the Dockerfile in the current folder

$ docker build -t {my_tag_name} --build-arg SSH_KEY="`cat ~/.ssh/id_rsa`" --no-cache .

List images

$ docker image ls
@fr-xiaoli
fr-xiaoli / exercise-equivalent-binary-trees.go
Created November 17, 2018 04:25
A Tour of Go - Concurrency - Exercise: Equivalent Binary Trees
package main
import (
"fmt"
"golang.org/x/tour/tree"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
@fr-xiaoli
fr-xiaoli / exercise-images.go
Created November 17, 2018 03:29
A Tour of Go - Methods and interfaces - Exercise: Images
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type Image struct{
w int
@fr-xiaoli
fr-xiaoli / exercise-rot-reader.go
Created November 17, 2018 02:47
A Tour of Go - Methods and interfaces - Exercise: rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@fr-xiaoli
fr-xiaoli / exercise-reader.go
Created November 17, 2018 02:29
A Tour of Go - Methods and interfaces - Exercise: Readers
package main
import "golang.org/x/tour/reader"
type MyReader struct{}
func (MyReader) Read(b []byte) (int, error) {
for i := 0; i < len(b); i++ {
b[i] = 'A'
}
@fr-xiaoli
fr-xiaoli / exercise-errors.go
Created November 17, 2018 02:09
A Tour of Go - Methods and interfaces - Exercise: Errors
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
@fr-xiaoli
fr-xiaoli / exercise-loops-and-functions.go
Created November 17, 2018 01:42
A Tour of Go - Basics - Exercise: Loops and Functions
package main
import (
"fmt"
)
func Sqrt(x float64) (z float64) {
z = x
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2*z)
@fr-xiaoli
fr-xiaoli / exercise-maps.go
Created November 17, 2018 01:31
A Tour of Go - Basics - Exercise: Maps
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) (m map[string]int) {
m = make(map[string]int)
ws := strings.Fields(s)
@fr-xiaoli
fr-xiaoli / exercise-stringer.go
Last active November 17, 2018 01:23
A Tour of Go - Methods and interfaces - Exercise: Stringers
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])