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
package mainimport "github.com/gin-gonic/gin"func main() {r := gin.Default()r.GET("/", func(c *gin.Context) {c.String(200, "Hello, Gin!")})r.Run(":8080")
}
安装vscode的插件错了
Installing github.com/ramya-rao-a/go-outline@latest FAILED
{"killed": false,"code": 1,"signal": null,"cmd": "E:\\Program Files\\Go\\bin\\go.exe install -v github.com/ramya-rao-a/go-outline@latest","stdout": "","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"
}1 tools failed to install.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
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"
解决方法
PS E:\golangwork> $env:GO111MODULE = "on"
PS E:\golangwork> $env:GOPROXY = "https://goproxy.cn"
PS E:\golangwork> go run golang.org/x/telemetry/cmd/gotelemetry@latest on
go: downloading golang.org/x/telemetry v0.0.0-20240522233618-39ace7a40ae7
go: downloading golang.org/x/sys v0.20.0
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.
查看版本
PS E:\golangwork> go version
go version go1.22.3 windows/amd64
hello代码
package mainimport "fmt"func main() {fmt.Println("Hello, World!")
}
需要先进行go mod init example/hello
查看mod文件
module example/hellogo 1.22.3
进行编译
go build hello.go
运行hello.exe
PS E:\golangwork\hello> ./hello
Hello, World!
或者直接运行go run hello.go