Skip to content

Instantly share code, notes, and snippets.

View smallnest's full-sized avatar

smallnest smallnest

View GitHub Profile
@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"
git config --global credential.modalPrompt false
```go
func isNil(i interface{}) bool {
return i == nil || reflect.ValueOf(i).IsNil()
}
```
better:
```go
func isNilFixed(i interface{}) bool {
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"strconv"
"sync"
# human readable /proc/net/netstat
# https://sa-chernomor.livejournal.com/9858.html
cat /proc/net/netstat | \
awk '(f==0) { i=1; while ( i<=NF) {n[i] = $i; i++ }; f=1; next} \
(f==1){ i=2; while ( i<=NF){ printf "%s = %d\n", n[i], $i; i++}; f=0} '
@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
@smallnest
smallnest / fork_and_daemon.go
Created April 20, 2020 06:52 — forked from wrfly/fork_and_daemon.go
golang fork and exec and handle signals
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
@smallnest
smallnest / gist:2bf9473c8fa9b66f4d32dd6087d94021
Created January 5, 2020 16:04 — forked from strickyak/gist:4201971
How to simulate classes and virtual methods in Golang.
/*
How to simulate classes and virtual methods in Go.
*/
package main
import . "fmt"
type A struct {
// Set My to outermost object (most derived class) after constructing.