Example #1
0
func MustGetExchangeRate(from string, to string) float64 {
	if !kmgStrings.IsAllAphphabet(from) {
		panic(fmt.Errorf("[MustGetExchangeRate] fromName [%s] should like USD", from))
	}
	if !kmgStrings.IsAllAphphabet(to) {
		panic(fmt.Errorf("[MustGetExchangeRate] toName [%s] should like USD", to))
	}
	if from == to {
		panic(fmt.Errorf("[MustGetExchangeRate] fromName [%s] =toName [%s] "))
	}
	out := kmgHttp.MustUrlGetContent(kmgHttp.MustSetParameterMapToUrl("https://query.yahooapis.com/v1/public/yql", map[string]string{
		"q":        fmt.Sprintf(`select * from yahoo.finance.xchange where pair="%s%s"`, from, to),
		"format":   "json",
		"env":      "store://datatables.org/alltableswithkeys",
		"callback": "",
	}))
	//out should look like {"query":{"count":1,"created":"2015-06-01T02:37:44Z","lang":"en","results":{"rate":{"id":"JPYCNY","Name":"JPY/CNY","Rate":"0.0501","Date":"6/1/2015","Time":"3:37am","Ask":"0.0501","Bid":"0.0501"}}}}
	resp := yqlRateResponse{}
	kmgJson.MustUnmarshal(out, &resp)
	if resp.Query.Results.Rate.Rate == "N/A" {
		panic(fmt.Errorf("[MustGetExchangeRate] Currency [%s][%s] not exist", from, to)) //有一种货币不存在.
	}
	rate, err := kmgStrconv.ParseFloat64(resp.Query.Results.Rate.Rate)
	if err != nil {
		panic(err)
	}
	return rate
}
Example #2
0
func (sdk *AliyunSDK) Call(param *url.Values) (r *AliyunRespond, isErr bool) {
	u := sdk.signature(param)
	resp, err := http.Get(u)
	handleErr(err)
	body, err := ioutil.ReadAll(resp.Body)
	handleErr(err)
	aliyunResp := &AliyunRespond{}
	kmgJson.MustUnmarshal(body, aliyunResp)
	if aliyunResp.Code != "" || aliyunResp.Message != "" {
		isErr = true
	}
	r = aliyunResp
	return
}
Example #3
0
func goRunInstall(goPath string, pathOrPkg string) {
	//只能更新本GOPATH里面的pkg,不能更多多个GOPATH里面其他GOPATH的pkg缓存.
	// go install 已知bug1 删除某个package里面的部分文件,然后由于引用到了旧的实现的代码,不会报错.删除pkg解决问题.
	// go install 已知bug2 如果一个package先是main,然后build了一个东西,然后又改成了非main,再gorun会使用旧的缓存/bin/里面的缓存.
	// TODO 已知bug3 当同一个项目的多个编译目标使用了同一个pkg,然后这个pkg变化了,缓存会出现A/B 问题,导致缓存完全无用.
	ctx := &goRunInstallContext{
		platform:          kmgPlatform.GetCompiledPlatform().String(),
		targetPkgMapCache: map[string]bool{},
		pkgMapCache:       map[string]*gorunCachePkgInfo{},
	}
	ctx.targetCachePath = filepath.Join(goPath, "tmp", "gorun", kmgCrypto.Md5HexFromString("target_"+pathOrPkg+"_"+ctx.platform))
	ctx.pkgCachePath = filepath.Join(goPath, "tmp", "gorun", "allPkgCache")
	ok := ctx.goRunInstallIsValidAndInvalidCache(goPath, pathOrPkg)
	if ok {
		//fmt.Println("use cache")
		return
	}
	//fmt.Println("not use cache")
	runCmdSliceWithGoPath(goPath, []string{"go", "install", pathOrPkg})
	// 填充缓存
	platform := ctx.platform
	ctx.targetPkgMapCache = map[string]bool{}
	outputJson := kmgCmd.CmdSlice([]string{"go", "list", "-json", pathOrPkg}).
		MustSetEnv("GOPATH", goPath).MustCombinedOutput()
	listObj := &struct {
		Deps []string
		Name string
	}{}
	kmgJson.MustUnmarshal(outputJson, &listObj)
	if listObj.Name != "main" {
		fmt.Printf("run non main package %s\n", pathOrPkg)
		return
	}
	listObj.Deps = append(listObj.Deps, pathOrPkg)
	for _, pkgName := range listObj.Deps {
		srcpkgPath := filepath.Join(goPath, "src", pkgName)
		fileList, err := kmgFile.ReadDirFileOneLevel(srcpkgPath)
		if err != nil {
			if !os.IsNotExist(err) {
				panic(err)
			}
			// 没有找到pkg,可能是这个pkg在GOROOT出现过,此处暂时不管.
			continue
		}
		ctx.targetPkgMapCache[pkgName] = true
		pkgInfo := &gorunCachePkgInfo{
			GoFileMap: map[string]uint64{},
		}
		for _, file := range fileList {
			ext := filepath.Ext(file)
			if ext == ".go" {
				pkgInfo.GoFileMap[file] = mustCheckSumFile(filepath.Join(srcpkgPath, file))
			}
		}
		pkgInfo.IsMain = pkgName == pathOrPkg
		pkgInfo.Name = pkgName
		pkgBinPath := pkgInfo.getPkgBinPath(goPath, platform)
		pkgInfo.PkgMd5 = mustCheckSumFile(pkgBinPath)
		ctx.pkgMapCache[pkgName] = pkgInfo
	}
	kmgGob.MustWriteFile(ctx.targetCachePath, ctx.targetPkgMapCache)
	kmgGob.MustWriteFile(ctx.pkgCachePath, ctx.pkgMapCache)
}