How can I run Effective GO source code in my local environment?

huangapple go评论116阅读模式
英文:

How can I run Effective GO source code in my local environment?

问题

我正在学习Go语言,我尝试在我的本地环境(GO 1.18+VS code)中运行Effective GO(https://go.dev/doc/effective_go#multiple-returns)的示例源代码。

例如:

  1. func nextInt(b []byte, i int) (int, int) {
  2. for ; i < len(b) && !isDigit(b[i]); i++ {
  3. }
  4. x := 0
  5. for ; i < len(b) && isDigit(b[i]); i++ {
  6. x = x*10 + int(b[i]) - '0'
  7. }
  8. return x, i
  9. }
  10. for i := 0; i < len(b); {
  11. x, i = nextInt(b, i)
  12. fmt.Println(x)
  13. }

另一个例子:

  1. func NewFile(fd int, name string) *File {
  2. if fd < 0 {
  3. return nil
  4. }
  5. f := new(File)
  6. f.fd = fd
  7. f.name = name
  8. f.dirinfo = nil
  9. f.nepipe = 0
  10. return f
  11. }

非常感谢提供一些提示!提前谢谢!

英文:

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).
For example

  1. func nextInt(b []byte, i int) (int, int) {
  2. for ; i &lt; len(b) &amp;&amp; !isDigit(b[i]); i++ {
  3. }
  4. x := 0
  5. for ; i &lt; len(b) &amp;&amp; isDigit(b[i]); i++ {
  6. x = x*10 + int(b[i]) - &#39;0&#39;
  7. }
  8. return x, i
  9. }
  10. for i := 0; i &lt; len(b); {
  11. x, i = nextInt(b, i)
  12. fmt.Println(x)
  13. }

Another example:

  1. if fd &lt; 0 {
  2. return nil
  3. }
  4. f := new(File)
  5. f.fd = fd
  6. f.name = name
  7. f.dirinfo = nil
  8. f.nepipe = 0
  9. return f
  10. }

Very appreciate give some Tips! Thanks in advance!

答案1

得分: 1

你需要创建一个名为main的包,并在其中创建一个main函数。

以下是一个main.go文件的示例:

  1. package main
  2. import "fmt"
  3. func isDigit(b byte) bool {
  4. // 在这里实现函数...
  5. }
  6. func nextInt(b []byte, i int) (int, int) {
  7. for ; i < len(b) && !isDigit(b[i]); i++ {
  8. }
  9. x := 0
  10. for ; i < len(b) && isDigit(b[i]); i++ {
  11. x = x*10 + int(b[i]) - '0'
  12. }
  13. return x, i
  14. }
  15. func main() {
  16. b := []byte{102, 97, 108, 99, 111, 110}
  17. for i := 0; i < len(b); {
  18. x, i = nextInt(b, i)
  19. fmt.Println(x)
  20. }
  21. }

要执行该程序,请运行:go run main.go

英文:

You need to create a package main and a main function inside of it

Here is an example of a main.go file

  1. package main
  2. import &quot;fmt&quot;
  3. func isDigit(b byte) bool {
  4. // function implementation here...
  5. }
  6. func nextInt(b []byte, i int) (int, int) {
  7. for ; i &lt; len(b) &amp;&amp; !isDigit(b[i]); i++ {
  8. }
  9. x := 0
  10. for ; i &lt; len(b) &amp;&amp; isDigit(b[i]); i++ {
  11. x = x*10 + int(b[i]) - &#39;0&#39;
  12. }
  13. return x, i
  14. }
  15. func main() {
  16. b := []byte{102, 97, 108, 99, 111, 110}
  17. for i := 0; i &lt; len(b); {
  18. x, i = nextInt(b, i)
  19. fmt.Println(x)
  20. }
  21. }

To execute run: go run main.go

huangapple
  • 本文由 发表于 2022年5月16日 09:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/72253257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定