草庐IT

go-mocket

全部标签

http - 向 Struct 添加一些日期,然后将其放入 go 模板中

我有一个文件controllers/catalog.go,它包含一个HTTP处理程序:funcCatalog(whttp.ResponseWriter,r*http.Request){ifr.Method!="GET"{http.Error(w,http.StatusText(405),http.StatusMethodNotAllowed)return}categories,err:=models.GetCategories()iferr!=nil{http.Error(w,http.StatusText(500),http.StatusInternalServerError)ret

go - 时间格式化程序的输出不正确

这个问题在这里已经有了答案:HowtoformatcurrenttimeusingayyyyMMddHHmmssformat?(6个答案)关闭6个月前。我正在尝试使用时间格式函数将时间格式化为MMMddyyyy,HH:mm:ss格式,但输出的日期不正确。下面是引用代码packagemainimport("fmt""time")funcmain(){varttime.Timet=time.Now()fmt.Println(t.Format("Jan012006,15:04:05"))//Output//Dec122018,16:27:34}但是如果我将引用格式更改为Jan022006,1

go - 如何在golang中用小内存读取大文件?

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我有这样的文档,我想循环读取大小为5GB的每个文件,我尝试了一些方法,例如(file,err:=ioutil.ReadFile(filename))但它会将整个文件加载到内存中。我使用这个函数来加载文件:funcvisit(files*[]string)filepath.WalkFunc{returnfunc(pathstring,infoos.FileInfo,errerror)error{if

go - 在 Go 中使用嵌入式类型的基

我是Go的新手,正在使用gowsdl生成的一组类型基于NetSuiteSuiteTalkwebservicedefinition.它创建了以下类型:typeBaseRefstruct{XMLNamexml.Name`xml:"urn:core_2018_2.platform.webservices.netsuite.comBaseRef"`Namestring`xml:"name,omitempty"`}typeRecordRefstruct{XMLNamexml.Name`xml:"urn:core_2018_2.platform.webservices.netsuite.comRe

go - 使用 JSON 的 POST 调用失败

这个问题在这里已经有了答案:Unmarshallingjsoningolang(1个回答)关闭4年前。我有一个接受这样的请求主体的API:内容类型是application/json。{"firstname":"foo","surname":"bar","age":10,"group":"test"}当我使用像Postman这样的客户端时,请求会通过。但是,来自Go的相同请求失败了:typeStudentstruct{firstnamestringsurnamestringageintgroupstring}student:=Student{"foo","bar",10,"test"}b

go - 在 Go 模板中显示可变图像

我在Go网络应用程序中使用一个模板,它应该根据访问者来自哪个国家/地区显示图像。图片我用的是FileServerhttp.Handle("/images/",http.StripPrefix("/images/",http.FileServer(http.Dir("images"))))在模板中传递了变量国家,因此应用程序知道要显示哪个标志。但是,由于某种原因,我传递的字符串添加了%0a,这导致img的src错误。预期的输出应该是以下代码用于抓取国家字符串resp3,err:=http.Get("https://ipinfo.io/country")iferr!=nil{fmt.Pri

go - gc Go 是如何处理堆分配的?

gcGo(特别是go1.11)是否预先分配了一block内存并在每次分配时从中获取(如JVM),或者它在每次创建变量时分配,它是内核调用(malloc)?如果每次分配一次内核调用,那将使创建变量变得昂贵。如何在堆栈/堆上强制分配? 最佳答案 这在很多地方都有介绍,比如theFAQ:HowdoIknowwhetheravariableisallocatedontheheaporthestack?Fromacorrectnessstandpoint,youdon'tneedtoknow.EachvariableinGoexistsasl

go - 如何使用Go执行“cat 7zSD.sfx.config.txtxxxx.7z> setup.exe”

Thisquestionalreadyhasanswershere:execgitcommandrefusestoredirectedtofileinGo(1个答案)goos/execcommandargumentissues[duplicate](1个答案)callingcommandwithsomeargumentsworksbutnotwithothersbutworksfromconsole(1个答案)Howtoexecutesystemcommandwithunknownarguments(3个答案)Howdoyougettheoutputofasystemcommandin

go - Type a type后,不能再混用

config.Config有一个方法funcString(string)string我想将这些方法混合到我的类型Config1.什么时候可以//typeConfigstructtypeConfigstruct{//astructhasmanymethodsconfig.ConfigPathstring}//newandcallstringmethodvaraConfig=&Config{}a.String("test")什么时候不行//typeConfigstructtype(Configureconfig.ConfigConfigstruct{ConfigurePathstring}

go - curl http 服务器没问题,但浏览器不起作用

我刚开始接触Golang中的网络应用。这是作为起点的简单代码:packagemainimport("fmt""log""net/http")const(CONN_HOST="localhost"CONN_PORT="8080")funchelloWorld(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"HelloWorld!")}funcmain(){http.HandleFunc("/",helloWorld)err:=http.ListenAndServe(CONN_HOST+":"+CONN_PORT,nil)iferr!