[英]How can I run Effective GO source code in my local environment?
I am beginning learn GO, I try to run the Effective GO ( https://go.dev/doc/effective_go#multiple-returns ) example source code in my local environment(GO 1.18+VS code).我开始学习 GO,我尝试在我的本地环境(GO 1.18+VS 代码)中运行 Effective GO ( https://go.dev/doc/effective_go#multiple-returns ) 示例源代码。 For example
例如
func nextInt(b []byte, i int) (int, int) {
for ; i < len(b) && !isDigit(b[i]); i++ {
}
x := 0
for ; i < len(b) && isDigit(b[i]); i++ {
x = x*10 + int(b[i]) - '0'
}
return x, i
}
for i := 0; i < len(b); {
x, i = nextInt(b, i)
fmt.Println(x)
}
Another example:另一个例子:
if fd < 0 {
return nil
}
f := new(File)
f.fd = fd
f.name = name
f.dirinfo = nil
f.nepipe = 0
return f
}
Very appreciate give some Tips!非常感谢给点提示! Thanks in advance!
提前致谢!
You need to create a package main and a main function inside of it您需要在其中创建一个 package main 和一个 main function
Here is an example of a main.go
file这是
main.go
文件的示例
package main
import "fmt"
func isDigit(b byte) bool {
// function implementation here...
}
func nextInt(b []byte, i int) (int, int) {
for ; i < len(b) && !isDigit(b[i]); i++ {
}
x := 0
for ; i < len(b) && isDigit(b[i]); i++ {
x = x*10 + int(b[i]) - '0'
}
return x, i
}
func main() {
b := []byte{102, 97, 108, 99, 111, 110}
for i := 0; i < len(b); {
x, i = nextInt(b, i)
fmt.Println(x)
}
}
To execute run: go run main.go
执行运行:
go run main.go
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.