草庐IT

function_substring-index

全部标签

function - 不带括号的 Golang 函数调用

我正在阅读他们网站上的Golang教程,我对我在此处简化和复制的类似代码感到困惑:packagemainimport("fmt""math")funcmain(){a:=math.Sqrt2fmt.Println(a)}这会在沙盒中打印1.4142135623730951。将a:=math.Sqrt2替换为a:=math.Sqrt(2)会做同样的事情,但我很困惑如何在没有括号的情况下调用该函数。math.Sqrt在这里不是函数指针(反正没有math.Sqrt2函数,它是一个没有任何括号传递的函数。Go文档here中的函数是列为:funcSqrt(xfloat64)float64即带有参

function - golang返回多个值问题

我想知道为什么这是有效的go代码:funcFindUserInfo(idstring)(Info,bool){it,present:=all[id]returnit,present}但这不是funcFindUserInfo(idstring)(Info,bool){returnall[id]}有没有办法避免临时变量? 最佳答案 详细说明我的comment,EffectiveGo提到访问映射键的多值分配称为“逗号确定”模式。Sometimesyouneedtodistinguishamissingentryfromazerovalue

google-app-engine - API 错误 4 (datastore_v3 : NEED_INDEX): no matching index found

我目前正尝试在Go中上传留言簿应用程序,该应用程序使用GAE的数据存储找到here.使用goappserve从我的计算机运行GAE服务器,应用程序运行正常。我提交了两个条目,并关闭了服务器。但是,在使用goappdeploy-applicationxxxapp.yaml后立即上传时,我的URL上出现APIerror4(datastore_v3:NEED_INDEX):nomatchingindexfound.。自从我上次提供文件以来已经大约一天了。感谢任何帮助 最佳答案 您可以删除Line41中的'.Order("-Date")',

elasticsearch - go + elastigo panic : runtime error: index out of range

我正在用elasticsearch测试golang我正在使用图书馆:https://github.com/mattbaird/elastigo我的问题是当我运行时:gorunelastigo_postal_code2.go编译器显示如下:panic:runtimeerror:indexoutofrangegoroutine1[running]:panic(0x893ce0,0xc82000a150)/opt/go/src/runtime/panic.go:464+0x3ffmain.main()/home/hector/go/elastigo_postal_code2.go:80+0x

function - 将值发送到另一个函数golang

我制作了一个包含4个房间的随机房间程序。我正在尝试获取每个房间的属性并将它们传递给其他功能。每个房间里都有一个人,有姓名和年龄属性。我正在尝试传递这些属性以针对if语句进行测试以发出额外的响应。我如何传递这些值?//therandommazeroomgamepackagemain//Allimportscanbecombinedinto()import("fmt"//Importneededforrandomoperation"math/rand"//Importrequiedtocalluponcurrenttime"time")typePersonstruct{Namestring

go - Rethinkdb,去 : Ensure Table and Index in one ReQL statement

我需要确保在应用程序启动时存在表。如果表不存在需要创建,我还想在表上创建二级索引。这在Go中很容易完成,但我想在ReQL中用一条语句完成。所以我想到了这个:funcensureTableIndex(ses*r.Session,namestring,indexstring)(errerror){err=r.TableList().Contains(name).Do(r.Branch(r.Row,r.Expr(nil),r.Do(func()r.Term{returnr.TableCreate(name).Do(func()r.Term{returnr.Table(name).IndexC

function - Go 的 struct 方法在调用中抛出太多参数

我在运行以下Go代码时遇到以下编译器错误。packagesorttypeInsertionSortstruct{Unsorted[]int;}func(isInsertionSort)Sort(modestring)[]int{length:=len(is.Unsorted);funcs:=map[string]func(int,int)bool{"method":is.greaterThan};ifmode=="desc"{funcs=map[string]func(int,int)bool{"method":is.lesserThan};}fori:=0;i=0&&funcs["m

function - 无法使用范围使用变量

我写了一个简单的脚本,它将读取/proc/cpuinfo并返回一个包含有关内核信息的[]map[string]string。问题是我无法使用范围内的值,它总是给我最后一个CPU的信息。我尝试在任何地方都使用闭包,但没有成功。我还尝试在循环中本地复制变量,但仍然没有成功。这是我的代码funcGetCpuInfo()CpuInfo{cpus,err:=os.Open("/proc/cpuinfo")iferr!=nil{log.Fatalln("Cannotopen/proc/cpuinfo")}defercpus.Close()s:=bufio.NewScanner(cpus)cpuCo

testing - golang 单元测试 : inner function parameters

我在go函数中的工作流程很简单,但是当进行单元测试时,我卡在了将参数传递给内部函数,或者模拟内部函数返回结果。代码:packagemyFuncimport(myPackagebookPackage)funcInit()(errerror){err=getResource(myPackage.GetPath())...}funcgetResource(pathstring)(errerror){//getresourcefrompath...err:=bookPackage.GetBook(path)}测试:packagemyFuncimport"testing"funcTestInit

function - Golang 函数测试

我正在使用第三方库,它是一些C函数的包装器。不幸的是,几乎所有的Go函数都是免费的(它们没有接收器,它们不是方法);不是我会采用的设计方法,但它就是我所拥有的。仅使用Go的标准“测试”库:是否有一种解决方案允许我创建可以模拟函数的测试?或者是将库包装成结构和接口(interface),然后模拟接口(interface)来实现我的目标的解决方案?我创建了一个蒙特卡洛模拟,它也处理生成的数据集。我的一种评估算法会寻找特定模型,然后将其传递给第三方函数进行评估。我知道我的边缘情况并且知道调用计数应该是多少,这就是我想要测试的。也许只需要一个简单的计数器?我发现使用这个库的其他项目没有完全覆盖