草庐IT

go - "invalid memory address or nil pointer deference"做教程

这是从教程中复制的确切代码。我是Go和web开发的新手,所以我很难调试此类错误。packagemainimport("fmt""html/template""log""net/http""strings")funcsayhelloName(whttp.ResponseWriter,r*http.Request){r.ParseForm()//Parseurlparameterspassed,thenparsetheresponsepacketforthePOSTbody(requestbody)//attention:IfyoudonotcallParseFormmethod,thef

function - Golang 文档中 "a"之前的 "..."和 "interface{}"是什么意思?

这个问题在这里已经有了答案:Meaningof...interface{}(dotdotdotinterface)(2个答案)关闭4年前。在官方文档中,经常出现如下代码。funcPrintf(formatstring,a...interface{})(nint,errerror)a和...分别是什么意思?

Golang 错误 "invalid memory address or nil pointer dereference",为什么会这样?

这个问题在这里已经有了答案:Go:panic:runtimeerror:invalidmemoryaddressornilpointerdereference(6个答案)关闭4年前。packagemainimport("fmt""net/http")funcmain(){http.HandleFunc("/",handler)http.HandleFunc("/cookie",cookie)http.ListenAndServe(":8080",nil)}funchandler(whttp.ResponseWriter,r*http.Request){//do...}funccooki

go - Golang内置函数 "make"如何实现不同类型的参数长度检查?

在文档中,API显示make接受类型和可变大小的IntegerType参数。funcmake(tType,size...IntegerType)Type做一个数组,我可以传3个参数,比如制作([]int,3,5)但是当我尝试制作map时make(map[int]int,3,5)当我编译时,它弹出toomanyargumentstomake(map[int]int)。这与编译器有关吗?是否可以为我自己的函数实现此行为? 最佳答案 编译器对make和其他内置函数有特殊的了解。编译器对make正在初始化的值类型强制执行允许的参数计数。编译

macos - 尝试从 Go 应用启动终端时出现 "exit status 1"

这个问题在这里已经有了答案:Howtoexecuteashellbuilt-incommand(2个答案)关闭3年前。我有一个名为myApp的非常简单的Go应用程序,它应该在macOS上启动一个新的终端窗口:packagemainimport("fmt""os/exec")funcmain(){err:=exec.Command("open","-a","Terminal","/Users/ns/go/").Run()iferr!=nil{fmt.Println(err)}}但是,当我运行该应用程序时,我得到以下输出:ns:~/go/src/github.com/nevadascout

go - Go程序"Import cycle not allowed"错误

我是Go的新手,我正在尝试制作一个程序来显示当前时间和其他一些内容://Aterribleprogram.packagemainimport("fmt""time")//greetingreturnsagreetingwithsomeinfo.funcgreeting()string{return"Helloflatworld,thetimeis:"+time.Now().String()}funcmain(){hotelName:="Trivag"hotelName+="o"fmt.Println(greeting())fmt.Println("Hotel:"+hotelName)}

go - 如何捕获 "slice bounds out of range"错误并为其编写句柄

我在stackoverflow中阅读了有关“范围的slice边界”的其他问题,但没有一个使用与此相同的上下文。然后,没有一个答案对我有帮助。在我的用例中,使用[]的子字符串的“golang语法”不会返回错误变量。它使用“panic”指令启动运行时错误。我的目标是避免达到“panic”指令。我需要处理此错误并提供消息来更详细地描述发生此错误时的上下文。观察:我需要获取子字符串值的字符串变量的内容是完全动态的,我用来获取子字符串值的索引同样是动态计算的。 最佳答案 您需要对索引进行边界检查:ifj>=0&&j

perl - 在 golang 中使用模板 "N/a* N/a*"解压由 perl 打包的缓冲区

我是perl的新手。我需要编写一个golang代码来读取从perlUDP套接字客户端发送的UDP包。基本上,perl客户端使用模板“N/a*N/a*”打包数据,如下所示:$them=pack($sockaddr,&AF_INET,$data_port,$broadaddr);$actual_data=pack("N/a*N/a*",$string1,$string2);send(S,$actual_data,0,$them)||die$!;我的问题是:“N/a*N/a*”究竟是什么意思?一个简单的解释会很有帮助。两个字符串实际上是如何打包的?如何在给定两个unicode字符串的情况下用

elasticsearch - 戈朗错误 "not enough arguments in call"

我刚接触golang。尝试通过golang实现批量上传到Elasticsearch。我正在使用golang库->https://github.com/olivere/elastic用于与Elasticsearch通信。此外,我正在尝试一段示例代码,但出现以下错误...suresh@BLR-245:~/Desktop/tools/golang/src$goinstallgithub.com/crazyheart/elastic-bulk-upload#github.com/crazyheart/elastic-bulk-uploadgithub.com/crazyheart/elasti

go - 在 golang 中获取日期,例如 "2018-07-13"

这个问题在这里已经有了答案:convertYYYYMMDDstringtoavaliddateinGo(2个答案)关闭4年前。如何像这样在golang中获取日期formate"2018-07-13"main.go文件packagemainimport("fmt""time")funcmain(){fmt.Println(time.Now())}$gorunmain.go输出:2018-07-1321:25:16.796379489+0500+05m=+0.000120455如何编写函数以像这样的格式“2018-07-13”在字符串中返回日期?