Skip to content

Instantly share code, notes, and snippets.

@chai2010
chai2010 / prime-gen.go
Last active August 29, 2015 14:05
Go语言的素数筛
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
// 返回生成自然数序列的管道: 2, 3, 4, ...
func GenerateNatural() chan int {
@chai2010
chai2010 / map.go
Created August 12, 2014 04:56
Go语言实现数组的Map函数
import (
"fmt"
"reflect"
)
func Map(slice interface{}, fn func(a interface{}) interface{}) interface{} {
val := reflect.ValueOf(slice)
out := reflect.MakeSlice(reflect.TypeOf(slice), val.Len(), val.Cap())
for i := 0; i < val.Len(); i++ {
@chai2010
chai2010 / error-bug.go
Created August 12, 2014 04:56
Go的error返回值相关的坑
package main
import "log"
type MyError struct{}
func (err *MyError) Error() string {
return "MyError"
}
@chai2010
chai2010 / my-int.go
Created August 12, 2014 05:01
Go语言变态用法
package main
import "fmt"
type int byte
func (p int) Foo() {
fmt.Printf("int.Foo: %v\n", p)
}
@chai2010
chai2010 / disable-main-exit.go
Created August 12, 2014 05:02
禁止main退出的方法
func main() {
defer func() { for {} }()
}
func main() {
defer func() { select {} }()
}
func main() {
defer func() { <-make(chan bool) }()
@chai2010
chai2010 / msvcbuild_lua.bat
Created August 12, 2014 06:33
MSVC编译lua的批处理文件
:: Script to build Lua with MSVC.
:: Copyright (C) 2010-2011 ChaiShushan<chaishushan{@}gmail.com>.
::
:: Either open a "Visual Studio .NET Command Prompt"
:: Then cd to this directory and run this script.
@if not defined INCLUDE goto :FAIL
@setlocal
cd %~dp0
@chai2010
chai2010 / db_lib.c
Created August 13, 2014 02:28
基于二进制文件的键值存储
// 数据库底层函数库
#include "db_lib.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
@chai2010
chai2010 / euler.c
Created August 13, 2014 05:19
整数{0, 1, 2, ... m-1}中有多少个数与m互素(最大公因子为1)?
#include <stdio.h>
#include <stdlib.h>
// return m*PI((p-1)/p);
int fEuler(int m, int p[]) {
int a = 1, b = 1, t = m; // a分子, b分母
for(int i = 0; ; ++i) {
div_t dt = div(t, p[i]);
// 如果break, 则t为素数
@chai2010
chai2010 / main.go
Created August 29, 2014 06:58 — forked from icub3d/main.go
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/golang/groupcache"
"github.com/ha/doozer"
"io"
@chai2010
chai2010 / designer.html
Created September 9, 2014 02:15
designer
<link rel="import" href="../topeka-elements/category-icons.html">
<link rel="import" href="../core-icon/core-icon.html">
<polymer-element name="my-element">
<template>
<style>
:host {
position: absolute;
width: 100%;