当前位置:   article > 正文

GoLang 安装

GoLang 安装

golang学习笔记

goland 安装

To use Go programming language in Visual Studio Code (VSCode), you can follow these steps:

1. Install Go: Download and install the latest version of Go from the official Go website (https://golang.org/dl/).

2. Install VSCode: Download and install Visual Studio Code from the official VSCode website (https://code.visualstudio.com/download).

3. Install the Go extension for VSCode: Open VSCode and go to the Extensions view by clicking on the square icon on the left sidebar or by pressing "Ctrl+Shift+X". Search for "Go" and click on the first result, which is the official Go extension from Microsoft. Click on the "Install" button to install the extension.

4. Configure the Go extension: After installing the extension, you'll need to configure it. Open the command palette by pressing "Ctrl+Shift+P" and search for "Go: Install/Update Tools". Select it and wait for the Go tools to be installed. Once finished, open the command palette again and search for "Go: Configure Workspace Settings". Select it and choose the option "Go" to generate a default configuration file for Go in your workspace.

5. Create a new Go file: Open a new file in VSCode by clicking on "File" > "New File". Save the file with the ".go" extension, for example, "main.go".

6. Write Go code: Start writing your Go code in the new file. You'll benefit from features such as IntelliSense, code navigation, and debugging provided by the Go extension.

7. Build and run Go code: To build and run your Go code, you can use the integrated terminal in VSCode. Open the terminal by clicking on "View" > "Terminal" or pressing "Ctrl+`". In the terminal, navigate to the directory where your Go file is located using the "cd" command. Then, use the "go build" command to build your code and the "./" followed by the executable name to run your code. For example:
```
cd /path/to/your/directory
go build
./main
```

用vscode编写一个gotest.go

  1. package main
  2. import "github.com/gin-gonic/gin"
  3. func main() {
  4. r := gin.Default()
  5. r.GET("/", func(c *gin.Context) {
  6. c.String(200, "Hello, Gin!")
  7. })
  8. r.Run(":8080")
  9. }

安装vscode的插件错了

  1. Installing github.com/ramya-rao-a/go-outline@latest FAILED
  2. {
  3. "killed": false,
  4. "code": 1,
  5. "signal": null,
  6. "cmd": "E:\\Program Files\\Go\\bin\\go.exe install -v github.com/ramya-rao-a/go-outline@latest",
  7. "stdout": "",
  8. "stderr": "go: github.com/ramya-rao-a/go-outline@latest: module github.com/ramya-rao-a/go-outline: Get \"https://goproxy.io/github.com/ramya-rao-a/go-outline/@v/list\": malformed HTTP response \"\\x00\\x00\\x12\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x80\\x00\\x04\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\xff\\xff\\xff\\x00\\x00\\x04\\b\\x00\\x00\\x00\\x00\\x00\\x7f\\xff\\x00\\x00\\x00\\x00\\b\\a\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\"\n"
  9. }
  10. 1 tools failed to install.
  11. go-outline: failed to install go-outline(github.com/ramya-rao-a/go-outline@latest): Error: Command failed: E:\Program Files\Go\bin\go.exe install -v github.com/ramya-rao-a/go-outline@latest
  12. go: github.com/ramya-rao-a/go-outline@latest: module github.com/ramya-rao-a/go-outline: Get "https://goproxy.io/github.com/ramya-rao-a/go-outline/@v/list": malformed HTTP response "\x00\x00\x12\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x80\x00\x04\x00\x01\x00\x00\x00\x05\x00\xff\xff\xff\x00\x00\x04\b\x00\x00\x00\x00\x00\x7f\xff\x00\x00\x00\x00\b\a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"

解决方法

  1. PS E:\golangwork> $env:GO111MODULE = "on"
  2. PS E:\golangwork> $env:GOPROXY = "https://goproxy.cn"
  3. PS E:\golangwork>
  4. go run golang.org/x/telemetry/cmd/gotelemetry@latest on
  5. go: downloading golang.org/x/telemetry v0.0.0-20240522233618-39ace7a40ae7
  6. go: downloading golang.org/x/sys v0.20.0
  7. Telemetry uploading is now enabled and data will be periodically sent to https://telemetry.go.dev/. Uploaded data is used to help improve the Go toolchain and related tools, and it will be published as part of a public dataset.

查看版本

  1. PS E:\golangwork> go version
  2. go version go1.22.3 windows/amd64

hello代码

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Println("Hello, World!")
  5. }

需要先进行go  mod init example/hello 

查看mod文件

  1. module example/hello
  2. go 1.22.3

进行编译

go build hello.go

运行hello.exe

  1. PS E:\golangwork\hello> ./hello
  2. Hello, World!

或者直接运行go run hello.go

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/926459
推荐阅读
相关标签
  

闽ICP备14008679号