简体   繁体   English

如何在我的本地环境中运行 Effective GO 源代码?

[英]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.

相关问题 Go代码在本地环境中运行结果与go play中运行结果不同 - Go code running result in local environment is not same with run in go play 如何在产品环境中保护我的GO REST服务? - How can I protect my GO REST services in product environment? 如何在我的 Mac 上的 Windows 平台上使用 Bazel 运行 Go 代码? - How can I run Go code with Bazel on Windows platform on my Mac? 如何在Android中运行我的Go代码? - How to run my Go code in Android? 转到如何在HTML页面上使用JavaScript代码 - Go How can I use JavaScript code on my html page 如何使用 Go 将我的代码编辑为并发? - How can I edit my code to concurrency using in Go? 如何在使用 Go Buffalo 框架的开发环境中使用 TOML fixtures 为我的数据库播种? - How can I use TOML fixtures to seed my database in a development environment using the Go Buffalo framework? 如何从源代码在我的ubuntu 12.10上安装Go - How to install Go on my ubuntu 12.10 from source code 如何在 Google Cloud Run 中运行 go-cloud-debug-agent,以便我可以在 Stackdriver Debug 中调试我的 go 应用程序 - How to run go-cloud-debug-agent in Google Cloud Run so that I can debug my go app in Stackdriver Debug 当我将 go mod 添加到我的项目中时,无法运行它,我该如何解决? - When i add go mod to my project, a can't run it, how can i fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM