go

Method does not change the value of object if the object is in a slice

笑着哭i 提交于 2021-02-08 10:19:41
问题 Here is my program: package main import ( "fmt" ) type Number struct { val int } func (num * Number) Increment () { num.val += 1 } func (num Number) Value() int { return num.val } func main() { numbers := []Number { {val: 12}, {val: 7}, {val: 0}, } for _, each := range numbers { each.Increment() fmt.Println(each.Value()) } for _, each := range numbers { fmt.Println(each.Value()) } } Here is the output: 13 8 1 12 7 0 First question: why does the Increment() method not update the value in the

Interface method return value of own type

和自甴很熟 提交于 2021-02-08 10:11:12
问题 I am trying to make a method which will take structs of a certain type and do operations on them. However, I need to have a method one can call on an instance of the stuct, and it will return objects of that struct's type. I am getting a compile time error because the return type of the type which implements the interface isn't the same as the interface's method return type, but that's because the interface needs to return values of it's own type. Interface declaration: type GraphNode

Interface method return value of own type

夙愿已清 提交于 2021-02-08 10:10:20
问题 I am trying to make a method which will take structs of a certain type and do operations on them. However, I need to have a method one can call on an instance of the stuct, and it will return objects of that struct's type. I am getting a compile time error because the return type of the type which implements the interface isn't the same as the interface's method return type, but that's because the interface needs to return values of it's own type. Interface declaration: type GraphNode

Access geolocation data using gomobile

那年仲夏 提交于 2021-02-08 09:28:44
问题 Can I access the phone's geolocation using a native gomobile application? I have seen experimental support for sensor data here, but nothing about the actual latitude, longitude of the device. I have no prior experience in app development, and I am learning Go at the moment. 回答1: AFAIK there is no ready solution yet to access location on both Android and iOS in a platform independent way. But in theory you could make separate packages using the gomobile tool to generate bindings for each

Is there a general name for Go types with “reference semantics” such as map, slice and channel?

陌路散爱 提交于 2021-02-08 09:25:12
问题 According to responses and comments in Go: reference types as arguments Go does not seem to have types that are "officially" called "reference types". Yet, there are some types that actually hold pointers to the underlying data which allows to efficiently pass values of these types as function arguments without taking pointer to them. When such value is passed as a function argument, there is no (or very little as with slices) copying of the underlying data so that there is no (or very little

how to keep subprocess running after program exit in golang?

一世执手 提交于 2021-02-08 09:11:21
问题 i noticed that subprocesses created using Start() will be terminated after program exit, for example: package main import "os/exec" func main() { cmd := exec.Command("sh", "test.sh") cmd.Start() } when main() exits, test.sh will stop running 回答1: The subprocess should continue to run after your process ends, as long as it ends cleanly, which won't happen if you hit ^C . What you can do is intercept the signals sent to your process so you can end cleanly. sigchan := make(chan os.Signal, 1)

Pipe a command and redirect the output with command

依然范特西╮ 提交于 2021-02-08 08:37:16
问题 Here is my code: package main import ( "fmt" "os" "os/exec" "strconv" "time" ) func main() { year, month, day := time.Now().Date() monthI := int(month) fmt.Println("toto") date := strconv.Itoa(year)+"_"+strconv.Itoa(monthI)+"_"+strconv.Itoa(day) nameSnapshot := "storedb@backup_"+date args := []string{"snapshot",nameSnapshot} cmd := exec.Command("zfs", args...) err := cmd.Run() if err != nil { os.Stderr.WriteString(err.Error()) } args = []string{"send",nameSnapshot,"|","gzip",">","backup_"

Does Go automatically close resources if not explicitly closed?

岁酱吖の 提交于 2021-02-08 08:27:22
问题 The following Open followed by a deferred Close is idiomatic in Go: func example() { f, err := os.Open(path) if err != nil { return } defer f.Close() } What happens if I don't have defer f.Close() ? When I call this function and f goes out of scope, does it automatically close the file or do I have a zombie file handle? If it closes automatically, when exactly does it do this? 回答1: It is true Files are closed when garbage collected, but... as mentioned in "Mystery of finalizers in Go" from

How to copy os.Stdout output to string variable

不羁的心 提交于 2021-02-08 08:13:22
问题 I have a function like this: package main import ( "fmt" ) // PrintSomething prints some thing func PrintSomething() { fmt.Println("print something") } func main() { PrintSomething() } How do I wrap PrintSomething to another function call CaptureSomething to save the string "print something" to a variable and return it? 回答1: Create pipe and set stdout to the pipe writer. Start a goroutine to copy the pipe reader to a buffer. When done, close the pipe writer and wait for goroutine to complete

How to import a private Go library (as module) within another private Go project (as module)

℡╲_俬逩灬. 提交于 2021-02-08 07:42:59
问题 I am moving a few private Go projects to GitLab while getting rid of Godeps , Go dep with vendor directory and all of that because I would like to use just Go modules. I am using Go version: go1.12.6 linux/amd64 . I have a private "library" project / git repository at gitlab.com/my-company/my-team/my-library . This works as a Go module with go.mod and go.sum . I would like to use my-library as a dependency for another project, this: gitlab.com/my-company/my-team/my-project . The structure of