Questions tagged «go»

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

2
进行两张地图的合并
我有一个递归函数,用于创建表示文件路径的对象(键是路径,值是有关文件的信息)。它是递归的,因为它仅用于处理文件,因此如果遇到目录,则会在目录上递归调用该函数。 话虽这么说,我想在两个映射上做一个集合并集的等效操作(即用递归调用中的值更新“主”映射)。除了遍历一个映射并将每个键,其中的值分配给另一映射中的相同对象之外,是否有惯用的方法来做到这一点? 那就是:给定a,b的类型map [string] *SomeObject,以及a和b最终被填充,有没有什么办法来更新a所有的值b?
81 map  go  union 



4
从goroutine捕获返回值
以下代码给出了编译错误,提示“意外运行”: x := go doSomething(arg) func doSomething(arg int) int{ ... return my_int_value } 我知道,如果正常调用函数就可以获取返回值,而无需使用goroutine。或者我可以使用频道等 我的问题是为什么不能从goroutine中获取像这样的返回值。

8
如何在不阅读的情况下检查通道是否关闭?
这是@Jimt在Go中编写的工作程序和控制器模式的一个很好的示例,以回答“在golang中是否有某种优雅的方式来暂停和恢复任何其他goroutine? ” package main import ( "fmt" "runtime" "sync" "time" ) // Possible worker states. const ( Stopped = 0 Paused = 1 Running = 2 ) // Maximum number of workers. const WorkerCount = 1000 func main() { // Launch workers. var wg sync.WaitGroup wg.Add(WorkerCount + 1) workers := …
80 go  channel 

4
Go中的空值
您如何在Go中表达“空”值? type Node struct { next *Node data interface{} } 我想说 return &Node{ data: NULL, next: NULL }
80 go 


2
访问go模块中的本地包(go 1.11)
我正在试用Go的新模块系统,无法访问本地软件包。以下项目位于我的gopath外的桌面上的文件夹中。 我的项目结构如下: / - /platform - platform.go - main.go - go.mod // platform.go package platform import "fmt" func Print() { fmt.Println("Hi") } // main.go package main import "platform" func main() { platform.Print() } go build main.go 告诉我 cannot find module for path platform
80 go  vgo 

2
关键字var后的下划线和接口名称是什么意思?
从http://golang.org/src/pkg/database/sql/driver/types.go中: type ValueConverter interface { // ConvertValue converts a value to a driver Value. ConvertValue(v interface{}) (Value, error) } var Bool boolType type boolType struct{} var _ ValueConverter = boolType{} // line 58 func (boolType) String() string { return "Bool" } func (boolType) ConvertValue(src interface{}) (Value, error) {....} 我知道ValueConverter是接口名称。第58行似乎声明了boolType实现接口ValueConverter,但这是否必要?我删除了58行,代码运行良好。


2
去安装做什么?
该文件说没有什么buildVSinstall呢 我的期望是,它像make install; 即,它将已编译的内容放入最终位置(/usr/local/bin/my_new_toy或其他位置),但似乎将内容放入GOROOT/bin 我可以告诉我去做make install-即把东西放在别处吗?还是只写一个makefile(请告诉我不)?
79 go 

5
如何从正在运行的Docker容器中读取文件和标准输出
我将如何在主机中启动应用程序以便从正在运行的Docker容器中读取文件和stdout? 本质上我想这样做: docker start containerid ./myapp // This app will *somehow* have access files and stdout generated by the container I just stared. 我将如何去做?更具体地说明我要在哪里进行此操作;我想读取Docker容器的日志和标准输出,并在其他地方处理这些日志。 我也愿意创建另一个可以从另一个容器读取文件和stdout的docker容器,但是我不知道这样做是否可行。
78 logging  go  docker 

7
从整数转换为二进制表示
有没有人知道Go中是否有任何内置功能可以将任何一种数字类型转换为其二进制数字形式。 例如,如果123是输入,则字符串"1111011"将是输出。
78 binary  go  numeric 

7
如何将数据库行转换为结构
假设我有一个结构: type User struct { Name string Id int Score int } 与数据库表具有相同的架构。将数据库行解析为结构的最简单方法是什么?我在下面添加了一个答案,但是我不确定这是最好的答案。
78 sql  go 

4
如何分析golang记忆?
我编写了一个golang程序,该程序在运行时使用1.2GB的内存。 调用将go tool pprof http://10.10.58.118:8601/debug/pprof/heap导致转储仅使用323.4MB的堆。 其余的内存使用情况又如何呢? 有没有更好的工具来解释golang运行时内存? 使用gcvis我得到这个: ..和此堆表单配置文件: 这是我的代码:https : //github.com/sharewind/push-server/blob/v3/broker

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.