草庐IT

method-group

全部标签

go - 为什么在method中只用一个字符来表示receiver?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion为什么在struct的方法中只用一个字符来表示当前实例?示例:typeSomethingstruct{}func(s*Something)doSomething(){}我发现使用起来更具可读性:func(something*Something)doSomething(){}

mongodb - 如何修复 : Golang "append" method pushing same elements to slice

我正在尝试将数据从DB(Mongo)映射到sliceingo,如果我返回简单的[]string一切正常,但如果我将类型更改为[]*models.Organization代码返回相同元素的slice。func(os*OrganizationService)GetAll()([]*models.Organization,error){varorganizations[]*models.Organizationresults:=os.MongoClient.Collection("organizations").Find(bson.M{})organization:=&models.Orga

methods - 在 golang 中,如何访问采用指针接收器且也在不同包中的方法?

packagetestsimport("testing""strconv""dir/model")typeTestStructstruct{IDintastringbstringcstringdstringacbooladbool}funcTestUpdate(t*testing.T){t.Log("Updating")cur:=TestStruct{i,a,b,c,d,true,true}err:=cur.model.Update(a,b,c,d,true,true)}在上面的代码块中,我试图调用一个方法,该方法采用接收者指针并且位于包“model”中。编译器错误是:引用未定义的字段

methods - Go:并发调用方法对我不起作用

这个问题在这里已经有了答案:Nooutputfromgoroutine(3个答案)关闭7年前。我是Go的新手。我正在尝试这个例子,我想从一个方法执行并发调用。这对我不起作用(我没有看到输出)。基于“EffectiveGo”,它说方法和函数支持并发。我做错了什么?谢谢,-斯里坎特packagemainimport("fmt")typeHellostruct{aint}func(h*Hello)Myprint(valuestring){gofunc(){fmt.Println(value)}()}funcmain(){h:=&Hello{100}goh.Myprint("needtogo"

go - 运行 dep 时出错确保 : Grouped write of manifest, 锁和 vendor :无法统计 VerifyVendor 声称存在的文件

运行depensure时出现以下错误:Groupedwriteofmanifest,lockandvendor:couldnotstatfilethatVerifyVendorclaimedexisted:stat"pathtopackageinsidevendor":nosuchfileordirectory这是我的Gopkg.toml:[[constraint]]name="github.com/PuerkitoBio/goquery"version="1.5.0"[[constraint]]branch="master"name="github.com/auth0-communi

http - Golang 'http.NewRequest(method, url, body)' 无法创建正确格式的请求

我正在尝试向以下api发送GET请求:https://poloniex.com/public?command=returnOrderBook带URL参数:currencyPair=BTC_ETHdepth=20-->¤cyPair=BTC_ETH&depth=20我尝试这样设置和执行我的请求:(请注意,为简洁起见,我删除了错误检查)pair:="BTC_ETH"depth:=20reqURL:="https://poloniex.com/public?command=returnOrderBook"values:=url.Values{"currencyPair":[]st

语言 : lack of contains method design-justification

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion在浏览包含方法时,我遇到了以下问答contains-method-for-a-slice在这个问答中多次提到这个方法实现起来真的很简单。我不明白的是,如果它如此容易实现,并且看到DRY是一种流行的软件原则&&并且大多数现代语言如何实现所述方法,排除这种简单方法的背后可能涉及什么样的设计推理?

正则表达式 : Grouping in a group

我正在尝试使用Regexp匹配Nmap命令的输出。可以有两种不同的格式。第一种格式(当nmap可以找到主机名时)Nmapscanreportfor2u4n32t-n4(192.168.2.168)和第二种格式(没有主机名)Nmapscanreportfor192.168.2.1我想同时捕获主机名和IP地址,如果没有主机名,就按照第二种格式获取Ip作为主机名。到目前为止,我在golang中尝试的正则表达式是Nmapscanreportfor\\s+([^[:space:]]+)(\\s+\\(([^[:space:]]+)\\))?但是我在golang中得到的结果第一种格式(它给了我(1

methods - 如何获取类型化函数的方法字段(Go)

我想知道是否可以使用反射或其他方式从类型化函数中获取方法字段。我要解决的问题是我有一个方法接受特定类型的函数,但我需要实际传输不同的类型并根据提供的类型执行操作。我知道我可以使用interface{}值作为接收者,但我不想放松对调用函数(“GetIt”)的类型检查packagemaintypettpstruct{Couponsstring}func(mttp)GetIt(xstring){ifm.Coupons!=""{print(m.Coupons)}}funccalculate(mthfunc(sstring)){//performcalculationsandupdatetheC

pointers - 调用结构函数给出 "cannot refer to unexported field or method"

我有这样的结构:typeMyStructstruct{Idstring}和函数:func(m*MyStruct)id(){//doingsomethingwithidhere}我还有一个这样的结构:typeMyStruct2struct{m*MyStruct}现在我有一个函数:funcfoo(str*MyStruct2){str.m.id()}但是我在编译时遇到错误:str.m.idundefined(cannotrefertounexportedfieldormethodmypackage.(*MyStruct)."".id如何正确调用这个函数? 最佳答案