Skip to content

Instantly share code, notes, and snippets.

View smallnest's full-sized avatar

smallnest smallnest

View GitHub Profile
@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
@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.
@smallnest
smallnest / how-does-async-work-in-async-std.md
Created December 2, 2019 09:01 — forked from Phaiax/how-does-async-work-in-async-std.md
Blog article: How does async work in async-std?

How does async work in async-std?

(Phaiax - 2019/12/1 - CC_BY_SA 4.0)

Lately I was porting a software from tokio/futures-1.0 to async-await. I somehow thought async-std was the successor of tokio and ported everything to async-std. 80% in, I noticed that my hyper dependency requires tokio and that it's not possible to replace tokio with async-std without also replacing hyper. Also, tokio and async-std try to solve the same problem. So I started a journey into the inners of the rust async story to find out if it is possible to use both tokio and async-std at the same time. (tl;dr: it is). I had heard of reactors and executors before, but there was much new stuff to discover.