Questions tagged «go»

Go是一种开源编程语言。它是静态类型的,具有从C松散派生的语法,添加了自动内存管理,类型安全性,一些动态类型输入功能,其他内置类型,例如可变长度数组(称为slices)和键值映射,以及大型标准库。

5
Go编译的二进制文件不会在Ubuntu主机上的高山Docker容器中运行
给定一个二进制文件,使用Go使用GOOS=linuxand编译该二进制文件,并将其GOARCH=amd64部署到docker基于的容器中alpine:3.3,如果Docker引擎主机为Ubuntu(15.10),则该二进制文件将不会运行: sh: /bin/artisan: not found 如果将docker引擎主机(作为的基础)部署在Mac OS X上的VirtualBox VM中,则该相同的二进制文件(针对相同的OS和Arch编译)将运行良好。busyboxalpine 如果容器基于Ubuntu映像之一,则同样的二进制文件也可以很好地运行。 知道这个二进制文件丢失了什么吗? 这是我所做的复制操作(未显示在OS X的VirtualBox / busybox中成功运行): 构建(即使拱门匹配,也将使用标志显式构建): ➜ artisan git:(master) ✗ GOOS=linux GOARCH=amd64 go build 检查它是否可以在主机上运行: ➜ artisan git:(master) ✗ ./artisan 10:14:04.925 [ERROR] artisan: need a command, one of server, provision or build 复制到docker dir,构建并运行: ➜ artisan git:(master) ✗ cp artisan …
78 go  docker  busybox  alpine 

5
mkdir(如果不存在)使用golang
我正在学习golang(beginner),并且一直在google和stackoverflow上搜索,但是找不到答案,所以请问一下是否可以打扰我,但是如果golang中不存在该怎么办? 例如,在节点中,我将使用带有功能sureDirSync的fs-extra (如果当然不关心阻塞) fs.ensureDir("./public");
78 go  directory  path  filepath  mkdir 

4
如何在Go中获取文件长度?
我查了 golang.org/pkg/os/#File,但还是不知道。似乎无法获取文件长度,我错过了什么吗? 如何在Go中获取文件长度?
78 file  go 

4
在Go中对多个返回值进行转换/类型声明的惯用方式
在Go中强制转换多个返回值的惯用方式是什么? 您可以单行执行吗,还是需要使用临时变量(例如我在下面的示例中所做的那样)? package main import "fmt" func oneRet() interface{} { return "Hello" } func twoRet() (interface{}, error) { return "Hejsan", nil } func main() { // With one return value, you can simply do this str1 := oneRet().(string) fmt.Println("String 1: " + str1) // It is not as easy with …

2
如何在Go中处理对/的不同方法的http请求?
我试图找出最好的方式来处理对Go的请求,/并且只能/以不同的方式处理不同的方法。这是我想出的最好的方法: package main import ( "fmt" "html" "log" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } if r.Method == "GET" { fmt.Fprintf(w, "GET, %q", html.EscapeString(r.URL.Path)) } else if r.Method == "POST" { fmt.Fprintf(w, "POST, %q", html.EscapeString(r.URL.Path)) } else …
77 go 

10
Golang将项目附加到切片
为什么切片a保持不变?会append()产生新的切片吗? package main import ( "fmt" ) var a = make([]int, 7, 8) func Test(slice []int) { slice = append(slice, 100) fmt.Println(slice) } func main() { for i := 0; i < 7; i++ { a[i] = i } Test(a) fmt.Println(a) } 输出: [0 1 2 3 4 5 6 …
77 go 


4
如何在Go中可靠地检测OS /平台
这是我当前正在使用的,我认为可以完成工作,但是必须有一种更好的方法: func isWindows() bool { return os.PathSeparator == '\\' && os.PathListSeparator == ';' } 如您所见,在我的情况下,我只需要知道如何检测窗口即可,但是我想知道检测任何平台/操作系统的方法。 玩: http://play.golang.org/p/r4lYWDJDxL

7
使用Content-Type multipart / form-data的POST数据
我正在尝试使用go将图像从计算机上传到网站。通常,我使用bash脚本将文件和密钥发送到服务器: curl -F "image"=@"IMAGEFILE" -F "key"="KEY" URL 它工作正常,但是我正在尝试将此请求转换为我的golang程序。 http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/ 我尝试了此链接和许多其他链接,但是,对于我尝试的每个代码,服务器的响应都是“未发送图像”,我也不知道为什么。如果有人知道上面的示例正在发生什么。


4
前往:如果唯一则追加
有没有一种方法可以检查切片/映射中是否存在值? 我只想在切片中不存在值时才将其添加到切片中。 这可行,但似乎很冗长。有更好的方法吗? orgSlice := []int{1, 2, 3} newSlice := []int{} newInt := 2 newSlice = append(newSlice, newInt) for _, v := range orgSlice { if v != newInt { newSlice = append(newSlice, v) } } newSlice == [2 1 3]
77 append  go  slice 

5
如何在golang切片中搜索元素
我有一部分结构。 type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a response body into the above slice if err := json.Unmarshal(respbody, &myconfig); err != nil { panic(err) } fmt.Println(config) 这是此的输出: [{key1 test} {web/key1 test2}] 我如何搜索此数组以获取元素在哪里key="key1"?
77 go  struct  slice 


3
如何使用httptest在Go中测试HTTP调用
我有以下代码: package main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "time" ) type twitterResult struct { Results []struct { Text string `json:"text"` Ids string `json:"id_str"` Name string `json:"from_user_name"` Username string `json:"from_user"` UserId string `json:"from_user_id_str"` } } var ( twitterUrl = "http://search.twitter.com/search.json?q=%23UCL" pauseDuration = 5 * time.Second ) func retrieveTweets(c chan<- …
76 http  go 

7
使用net / http设置cookie
我正在尝试使用Go的net / http包设置cookie。我有: package main import "io" import "net/http" import "time" func indexHandler(w http.ResponseWriter, req *http.Request) { expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{"test", "tcookie", "/", "www.domain.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}} req.AddCookie(&cookie) io.WriteString(w, "Hello world!") } func main() { http.HandleFunc("/", indexHandler) http.ListenAndServe(":80", nil) } 我尝试将“ Golang”与“ …
76 cookies  go 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.