Archiving a bunch of old stuff
This commit is contained in:
51
oldstuff/gotime/junkdrawer/greppish/main.go
Normal file
51
oldstuff/gotime/junkdrawer/greppish/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const USAGE = `Usage: greppish <regex> [filename]
|
||||
|
||||
Prints lines matching <regex> in [filename] or standard input.`
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println(USAGE)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
regMatch, err := regexp.Compile(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatal("Problem compiling regex: ", err)
|
||||
}
|
||||
|
||||
inbuf := bufio.NewReader(os.Stdin)
|
||||
|
||||
if len(os.Args) > 2 {
|
||||
infile, err := os.Open(os.Args[2])
|
||||
if err != nil {
|
||||
log.Fatal("Problem opening input file: ", err)
|
||||
}
|
||||
inbuf = bufio.NewReader(infile)
|
||||
}
|
||||
|
||||
var line string
|
||||
|
||||
for {
|
||||
line, err = inbuf.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
os.Exit(0)
|
||||
}
|
||||
log.Fatal("Problem reading input: ", err)
|
||||
}
|
||||
if regMatch.MatchString(line) {
|
||||
fmt.Printf(line)
|
||||
}
|
||||
}
|
||||
}
|
52
oldstuff/gotime/junkdrawer/randimg/main.go
Normal file
52
oldstuff/gotime/junkdrawer/randimg/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"flag"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
outfile = flag.String("o", "randimg.png", "Output PNG file name")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
|
||||
out, err := os.Create(*outfile)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
imgRect := image.Rect(0, 0, 200, 200)
|
||||
img := image.NewGray(imgRect)
|
||||
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
|
||||
|
||||
for y := 0; y < 200; y += 10 {
|
||||
for x := 0; x < 200; x += 10 {
|
||||
fill := &image.Uniform{color.Black}
|
||||
if rand.Intn(10) % 2 == 0 {
|
||||
fill = &image.Uniform{color.White}
|
||||
}
|
||||
|
||||
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
|
||||
}
|
||||
}
|
||||
|
||||
err = png.Encode(out, img)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
fmt.Printf("Wrote random image to \"%s\"\n", *outfile)
|
||||
os.Exit(0)
|
||||
}
|
41
oldstuff/gotime/junkdrawer/reflectish/main.go
Normal file
41
oldstuff/gotime/junkdrawer/reflectish/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type Fooer interface {
|
||||
Foo(int) int
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
SomeField int
|
||||
}
|
||||
|
||||
func (this *Bar) Foo(x int) int {
|
||||
return x
|
||||
}
|
||||
|
||||
type Baz struct {
|
||||
SomeField int
|
||||
}
|
||||
|
||||
func (this Baz) Foo(x int) int {
|
||||
return x
|
||||
}
|
||||
|
||||
func main() {
|
||||
var bar, baz Fooer
|
||||
|
||||
bar = &Bar{
|
||||
SomeField: 5,
|
||||
}
|
||||
|
||||
baz = Baz{
|
||||
SomeField: 5,
|
||||
}
|
||||
|
||||
fmt.Println(reflect.ValueOf(baz).FieldByName("SomeField"))
|
||||
fmt.Println(reflect.Indirect(reflect.ValueOf(bar)).FieldByName("SomeField"))
|
||||
}
|
Reference in New Issue
Block a user