Skip to content

Instantly share code, notes, and snippets.

View smallnest's full-sized avatar

smallnest smallnest

View GitHub Profile
@smallnest
smallnest / scratch_server.go
Created April 23, 2021 02:05 — forked from jschaf/scratch_server.go
A Go web server from scratch using syscalls
package main
// Simple, single-threaded server using system calls instead of the net library.
//
// Omitted features from the go net package:
//
// - TLS
// - Most error checking
// - Only supports bodies that close, no persistent or chunked connections
// - Redirects
@smallnest
smallnest / benchmark_timers.go
Created January 14, 2021 10:09 — forked from draveness/benchmark_timers.go
Benchmark Golang Timers
package main
import (
"fmt"
"sort"
"sync"
"testing"
"time"
)
@smallnest
smallnest / LearnGoIn5mins.md
Created January 14, 2021 03:40 — forked from prologic/LearnGoIn5mins.md
Learn Go in ~5mins
@smallnest
smallnest / piping.go
Created December 27, 2020 05:33 — forked from dagoof/piping.go
piping exec.Cmd in golang (example finds most recently modified file that is not directory or executable)
package main
import (
"os"
"exec"
)
func pipe_commands(commands ...*exec.Cmd) ([]byte, os.Error) {
for i, command := range commands[:len(commands) - 1] {
out, err := command.StdoutPipe()
if err != nil {
@smallnest
smallnest / main.go
Created December 25, 2020 07:43 — forked from Skarlso/main.go
Golang SSH connection with hostkey verification
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"golang.org/x/crypto/ssh"
kh "golang.org/x/crypto/ssh/knownhosts"
@smallnest
smallnest / sshclient.go
Created December 25, 2020 03:43 — forked from josephspurrier/sshclient.go
Golang SSH Client
package main
import (
"bufio"
"io/ioutil"
"os/signal"
//"syscall"
"fmt"
"log"
"os"
@smallnest
smallnest / sshclient.go
Created December 25, 2020 03:43 — forked from Mebus/sshclient.go
Golang SSH Client
package main
import (
"bufio"
"io/ioutil"
"os/signal"
//"syscall"
"fmt"
"log"
"os"
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"strconv"
"sync"
@smallnest
smallnest / use_c_sleep.go
Created May 16, 2020 13:05 — forked from jaypei/use_c_sleep.go
Golang的调度机制决定Sleep是用户态实现的。但实现存在一个问题,若go程序在Sleep的过程中系统时间发生变化,Sleep会不准确,因为runtime中的实现是通过时间列表判断每个goroutine的挂起时间,而这个时间就是用的系统时间。这段代码是用cgo调用C的sleep规避这个问题的实现。
package main
/*
#include <unistd.h>
*/
import "C"
import (
"fmt"
"time"
)
@smallnest
smallnest / startProcess.go
Created April 20, 2020 10:50 — forked from lee8oi/startProcess.go
Using os.StartProcess() in Go for platform-independent system command execution.
package main
import (
"os"
"os/exec"
)
func Start(args ...string) (p *os.Process, err error) {
if args[0], err = exec.LookPath(args[0]); err == nil {
var procAttr os.ProcAttr