Documentation ¶
Overview ¶
Package utils - 套件所需的公用工具(總和、平均、序列差、持續天數、 民國日期解析、簡單亂數、標準差、簡單 net/http 快取).
Index ¶
- Constants
- Variables
- func AvgFloat64(data []float64) float64
- func AvgUint64(data []uint64) uint64
- func CalDiffFloat64(listA, listB []float64) []float64
- func CalDiffInt64(listA, listB []int64) []int64
- func CalLHPower(final, low, high []float64) []float64
- func CountCountineFloat64(data []float64) (int, bool)
- func DeltaFloat64(data []float64) []float64
- func DeltaInt64(data []int64) []int64
- func GetOSRamdiskPath(goos string) string
- func ParseDate(strDate string) time.Time
- func RandInt() int
- func SD(list []float64) float64
- func SDUint64(list []uint64) float64
- func SumFloat64(data []float64) float64
- func SumUint64(data []uint64) uint64
- func ThanPastFloat64(data []float64, days int, max bool) bool
- func ThanPastUint64(data []uint64, days int, max bool) bool
- func ThanSumPastFloat64(data []float64, days int, max bool) bool
- func ThanSumPastUint64(data []uint64, days int, max bool) bool
- type HTTPCache
Examples ¶
Constants ¶
const ( TWSEURL string = "http://mis.twse.com.tw" TWSEHOST string = "http://www.twse.com.tw" OTCHOST string = "http://www.tpex.org.tw" HOME string = "/stock/index.jsp" OTCCSV string = "/ch/stock/aftertrading/daily_trading_info/st43_download.php?d=%d/%02d&stkno=%s&r=%%d" // year, mon, stock, rand OTCLISTCSV string = "/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430_download.php?l=zh-tw&d=%s&se=%s&s=0,asc,0" // date, cate TWSECSV string = "/ch/trading/exchange/STOCK_DAY/STOCK_DAYMAIN.php" TWSELISTCSV string = "/ch/trading/exchange/MI_INDEX/MI_INDEX.php" // year, mon, day, type TWSEREAL string = "/stock/api/getStockInfo.jsp?ex_ch=%s_%s.tw&json=1&delay=0&_=%d" QFIISTOP20 string = "/ch/trading/fund/MI_QFIIS_sort_20/MI_QFIIS_sort_20.php" // yyyymmdd BFI82U string = "/ch/trading/fund/BFI82U/BFI82U_print.php?begin_date=%d%02d%d&end_date=%d%02d%d&report_type=day&language=ch&save=csv" // yyyymmdd T86 string = "/ch/trading/fund/T86/T86.php" TWTXXU string = "/ch/trading/fund/%s/%s.php?download=csv&qdate=%d/%02d/%02d&sorting=by_issue" S3CSV string = "https://s3-ap-northeast-1.amazonaws.com/toomore/gogrs/list.csv" )
TWSE base url.
const TempFolderName = ".gogrscache"
TempFolderName 快取資料夾名稱
Variables ¶
var ExchangeMap = map[string]bool{"tse": true, "otc": true}
ExchangeMap is simple to check tse or otc.
var HTTPClient = &http.Client{Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, Dial: (&net.Dialer{ Timeout: 0, KeepAlive: 0, }).Dial, TLSHandshakeTimeout: 1 * time.Second, }, }
HTTPClient is default func and fixed http too many open files.
var TaipeiTimeZone = time.FixedZone("Asia/Taipei", 8*3600)
TaipeiTimeZone is for time.Data() setting.
Functions ¶
func AvgFloat64 ¶
AvgFloat64 計算平均(float64)
Example ¶
fmt.Println(AvgFloat64([]float64{1.1, 2.2, 9.9}))
Output: 4.4
func AvgUint64 ¶
AvgUint64 計算平均(uint64)
Example ¶
fmt.Println(AvgUint64([]uint64{1, 2, 9}))
Output: 4
func CalDiffFloat64 ¶
CalDiffFloat64 計算兩序列的差(listA - listB)
Example ¶
var sample1 = []float64{1.1, 2.2, 3.3, 4.4, 5.5} var sample2 = []float64{5.5, 4.4, 3.3, 2.2, 2.2} fmt.Println(CalDiffFloat64(sample1, sample2))
Output: [-4.4 -2.2 0 2.2 3.3]
func CalDiffInt64 ¶
CalDiffInt64 計算兩序列的差(listA - listB)
Example ¶
var sample1 = []int64{1, 2, 3, 4, 5} var sample2 = []int64{5, 4, 3, 2, 2} fmt.Println(CalDiffInt64(sample1, sample2))
Output: [-4 -2 0 2 3]
func CountCountineFloat64 ¶
CountCountineFloat64 計算最後一個數值為正或負值的持續天數(float64)
Example ¶
var sample1 = []float64{-1, 0, 1, 2, 3} fmt.Println(CountCountineFloat64(sample1)) var sample2 = []float64{1, 0, -1, -2, -3} fmt.Println(CountCountineFloat64(sample2))
Output: 3 true 3 false
func DeltaFloat64 ¶
DeltaFloat64 計算數列間距差
Example ¶
fmt.Println(DeltaFloat64([]float64{-1.1, 0, 1.1, 2.2, 3.3}))
Output: [1.1 1.1 1.1 1.0999999999999996]
func DeltaInt64 ¶
DeltaInt64 計算數列間距差
Example ¶
fmt.Println(DeltaInt64([]int64{-1, 0, 1, 2, 3}))
Output: [1 1 1 1]
func GetOSRamdiskPath ¶
GetOSRamdiskPath try to get RamDisk path.
func ParseDate ¶
ParseDate is to parse "104/01/13" format.
Example ¶
fmt.Println(ParseDate("104/09/29")) fmt.Println(ParseDate("104/9/29")) fmt.Println(ParseDate("104/9/2"))
Output: 2015-09-29 00:00:00 +0800 Asia/Taipei 2015-09-29 00:00:00 +0800 Asia/Taipei 2015-09-02 00:00:00 +0800 Asia/Taipei
func ThanPastFloat64 ¶
ThanPastFloat64 計算最後一個數值是否為過去幾天最大或最小(float64)
func ThanPastUint64 ¶
ThanPastUint64 計算最後一個數值是否為過去幾天最大或最小(uint64)
func ThanSumPastFloat64 ¶
ThanSumPastFloat64 計算最後一個數值是否為過去幾天的總和大或小(float64)
Types ¶
type HTTPCache ¶
type HTTPCache struct { Dir string // contains filtered or unexported fields }
HTTPCache net/http 快取功能
Example ¶
目前可以支援 http.Get / http.PostForm 取得資料並儲存
hc := NewHTTPCache("/run/shm/", "utf8") // linux ex1, _ := hc.Get("http://httpbin.org/get", false) ex2, _ := hc.Get("http://httpbin.org/get?q=%d", true) ex3, _ := hc.PostForm("http://httpbin.org/post", url.Values{"name": {"Toomore"}}) fmt.Printf("%s %s %s", ex1, ex2, ex3)
Output: