草庐IT

引用计数

全部标签

arrays - 即使初始化也没有指针取消引用

我的目标是声明一个空的二维数组,然后在每次do()运行时对其进行初始化并填充值。问题是即使我正在初始化数组,我也会得到一个nil指针取消引用。这是我试图在服务器模拟器上完成的一个简单版本。packagemainimport"fmt"typeSrvstruct{A*[][]int}func(sSrv)init(){arr:=make([][]int,0)*s.A=arr}funcmain(){s:=Srv{nil}s.init()printSlice(*s.A)do(s.A)do(s.A)}funcprintSlice(s[][]int){fmt.Printf("len=%dcap=%d

go - Go中的Supertype如何引用Subtype

Go不支持多态性。如果要在通用类型的保护伞下传递特定类型,则它无法在Go中工作。以下一段代码抛出错误。在Go中实现相同功能的最佳方法是什么?packagemainimport("fmt")typeparentstruct{parentIDstring}typechild1struct{parentchild1IDstring}typechild2struct{parentchild2IDstring}typechildCollectionstruct{collection[]parent}func(c*childCollection)appendChild(pparent){c.col

go - 通过范围函数中的地址引用

我有一个定义如下的OuterStruct结构。我正在使用该函数初始化OuterStruct->InnerStruct值name和Var1insetup1()和Var1在setup2()中。在setup2()中分配的值总是nil因为它是按值引用它的。如何使用在这里引用我的地址?typeInnerStructstruct{NamestringVar1*api.Var1Var2*ap1.Var2}typeOuterStructstruct{opNamestringMyData[]InnerStructLogDirstring}func(obj*OuterStruct)Setup2(){for

go - 指针引用未存储在我的 go 程序中的结构中

我是go-lang的新手,我试图弄清楚如何正确地使用结构和依赖注入(inject)。我有点卡住了,因为我无法正确存储对另一个结构的引用。这是我生成CommandController的方法。存在对iris.Application的有效引用。funcProvideCommandController(application*iris.Application,commandRepositorycommand.CommandRepository)(*interfaces.CommandController,error){commandController:=interfaces.CommandC

variables - 使用其他变量中的数据引用go中的变量?

我对go/编码比较陌生。我希望能够通过使用变量来引用变量。vara=make([]int,0)varb=make([]int,0)varc=make([]int,0)我定义了一些sliceset:=input.Ask("Whichsetwouldyouliketoinputto(a,b,c):")fortrue{num:=input.Ask("Number:")strings.toLower(set)=append(strings.toLower(set),num)}我希望“strings.tolower”部分输出一个字符串,它确实如此,这将允许我选择我定义的变量之一。

http - 运行一个简单的服务器,但计数器似乎增加了 3,为什么?

这个问题在这里已经有了答案:Whythissimplewebserveriscalledevennumbertimes?(1个回答)关闭6年前。我这里有这个小服务器。目的是如果我访问localhost:8000/*它应该将counter加1,如果我访问localhost:8000/count,它应该显示counter。发生的一件奇怪的事情是,似乎每次我访问localhost:8000时,计数器都会增加3。所以我会转到localhost:8000/count和counter将在3,然后我访问localhost:8000,然后再次访问localhost:8000/count,counter

string - golang int 对字符串的引用

请帮助我理解这一点,也许我做错了什么。funcmain(){x:=6y:=&xfmt.Println("x:",x,",y:",*y,"stringy:",string(*y))}返回:x:6,y:6stringy:为什么string(*y)不返回6? 最佳答案 specificationsays:ConvertingasignedorunsignedintegervaluetoastringtypeyieldsastringcontainingtheUTF-8representationoftheinteger.表达式string

go - 从另一个包引用结构域

我具有以下文件夹结构:.├──Makefile├──README.md├──myproject│  ├──handlers│  │  └──authorize_handler.go│  ├──models│  │  ├──id_token.go│  ├──server.go我尝试从authorize_handler.go引用IdToken.idType文件中的id_token.go字段。authorize_handler.gopackagehandlersimport("encoding/json""log""net/http""myproject/models")funcAuthor

测试 HTTP 请求时出现 Nil 取消引用错误

我正在尝试在我的Go库中测试HTTP请求。进行调用的对象通过依赖注入(inject)接受HTTP客户端对象,因此在我的测试中,我像这样模拟HTTP客户端:funcTestMyObject(t*testing.T){server:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){w.WriteHeader(200)w.Header().Set("Content-Type","application/json")fmt.Fprintln(w,mockJSONResponse)}))d

go - 引用库中的另一个.go文件

我是Go的新手,我已经经历了HowtoWriteGoCode虽然它非常有帮助,但我对如何使用同一个库中的go文件感到困惑。例如,这是我的结构:~/src/hashtable/hashtable.golinkedlist.go我想在哈希表中使用链表。我的目录结构应该是什么,我应该使用什么包名? 最佳答案 在Go中,两个或多个具有相同包名称的文件被视为一个包,这意味着在命名空间内可以访问所有内容,包括私有(private)(小写)和公共(public)(大写)符号。例如,如果hashtable.go和linkedlist.go共享相同的