Skip to content

Instantly share code, notes, and snippets.

@physacco
physacco / test_log.go
Created March 10, 2013 06:14
This demonstrates usage of the log package in go. Log to stderr or a file.
package main
import (
"os"
"log"
"time"
)
func main() {
logfile, err := os.OpenFile("test.log",
@physacco
physacco / test_periodic_timer.go
Created March 10, 2013 06:52
This demonstrates usage of timer in go. Both one-shot timer and periodic timer. In the periodic timer example, goroutine and select are used.
package main
import (
"fmt"
"time"
)
// The timeout handler.
func ontimeout(t time.Time) {
fmt.Println("[timeout] ", t)
@physacco
physacco / test_json_decoder.go
Last active December 14, 2015 18:59
Test the JSON decoder in go's stdlib by parsing package.json of a nodejs project.
package main
import (
"os"
"fmt"
"encoding/json"
)
type PackageInfo struct {
Name string
@physacco
physacco / test_regexp.go
Created March 11, 2013 11:45
This is a basic example of the regexp package in go's stdlib.
package main
import (
"fmt"
"regexp"
)
func main() {
str := "Ruby 2.0 is the 2nd major release of Ruby"
re, err := regexp.Compile("[.\\d]+")
@physacco
physacco / simplerouter.go
Last active December 14, 2015 19:09
This is a bidirectional router that shows how to implement I/O multiplexing in Go. The function is similar to netcat.
/*
* This is a bidirectional router that shows how to implement
* I/O multiplexing in Go. The function is similar to netcat.
* Test this program in terminal:
*
* $ go run simplerouter.go
* GET / HTTP/1.0<CR>
* <CR>
* HTTP/1.0 302 Found
* ...
@physacco
physacco / simplerouter2.go
Created March 12, 2013 03:49
This is a bidirectional router that shows how to implement I/O multiplexing in Go. The function is similar to netcat. Different from simplerouter.go, it uses 2 goroutines to route (si, no) and (ni, so). Thus neither direction blocks.
/*
* This is a bidirectional router that shows how to implement
* I/O multiplexing in Go. The function is similar to netcat.
* Test this program in terminal:
*
* $ go run simplerouter.go
* GET / HTTP/1.0<CR>
* <CR>
* HTTP/1.0 302 Found
* ...
@physacco
physacco / netcat.go
Created March 12, 2013 04:38
This is a simple netcat implementation in go.
/*
* This is a simple netcat implementation in go.
* Test this program in terminal:
*
* $ go run simplerouter.go
* GET / HTTP/1.0<CR>
* <CR>
* HTTP/1.0 302 Found
* ...
*
@physacco
physacco / test_timeout.go
Created March 13, 2013 01:49
This code shows how to add a timeout to your function in go.
package main
import (
"fmt"
"time"
)
func work(ch chan int) {
time.Sleep(5000 * time.Millisecond)
ch <- 12345
@physacco
physacco / socks5.go
Created March 13, 2013 13:55
This is a high performance socks5 proxy server written in go. It is much faster than those I wrote before.
package main
import (
"io"
"os"
"fmt"
"log"
"net"
"time"
"strings"
@physacco
physacco / test_signal.go
Created March 14, 2013 05:07
A demo of signal handling in go.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {