假設(shè)我有一個(gè)方法 http.HandleFunctype HandlerFunc func(ResponseWriter, *Request)我想將它包裝在另一個(gè)相同類型的方法周圍,如下所示:func MyWrapper(res http.ResponseWriter, req *http.Request) { // do stuff AnotherMethod(res, req) // <- question refers to this line // do more stuff}func AnotherMethod(res http.ResponseWriter, req *http.Request) { // main logic}如果我做對(duì)了,當(dāng)我打電話時(shí),我將(的值)AnotherMethod(res, req)的副本傳遞res給AnotherMethod,這意味著該對(duì)象現(xiàn)在在內(nèi)存中重復(fù)了。有沒(méi)有一種方法可以將指針傳遞res到AnotherMethod,然后在其中取消引用,以便不復(fù)制的值res?還是我不明白某事?(使用指向res內(nèi)部(的值)的指針AnotherMethod將不起作用,因?yàn)閕nhttp.ResponseWriter中所有方法的接收者都是值而不是指針)
Go中傳遞的參數(shù)
慕尼黑8549860
2021-04-25 17:19:07