Skip to content

Commit

Permalink
update 2025年 01月 19日 星期日 13:39:46 CST
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrysisa committed Jan 19, 2025
1 parent 73f5eb1 commit 65f3073
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
47 changes: 34 additions & 13 deletions content/posts/go/go-class.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Tutorial: Go Class"
title: "Go Class"
subtitle:
date: 2024-12-22T23:23:53+08:00
slug: 60f4958
Expand Down Expand Up @@ -53,27 +53,35 @@ repost:

## Hello world!

### Install

Installation: [Download and install](https://go.dev/doc/install)

```sh
# Select the version for your computer's operating system
# Then remove any previous go and create a fresh Go tree in `/usr/local/`
$ tar -zxvf /path/to/go<version>.<os>-<arch>.tar.gz
# Create symbol links of programs (go and gofmt) in `go/bin/`
$ sudo ln -s /path/to/go/bin/* /usr/local/bin/
# Uninstall previous go
$ rm -rf /usr/local/go

# Install go
$ tar -C /usr/local -xzf go<version>.<os>-<arch>.tar.gz

# Add path in config of shell e.g. ~/.bashrc or /etc/bash.bashrc
export PATH=$PATH:/usr/local/go/bin

# Test go version
$ go version
go version go<version> <os>/<Arch>
```

接下来运用 `go env` 命令找到那些设定在 $HOME 目录下的环境变量,将其修改为指定地址:
### Configuration

接下来运用 `go env` 命令找到那些设定在 `$HOME` 目录下的环境变量,将其修改为指定地址:

```sh
$ go env | grep "$HOME/go"
GOMODCACHE='<$HOME>/go/pkg/mod'
GOPATH='<$HOME>/go'
$ go env -w GOMODCACHE='<$HOME>/.go/pkg/mod'
$ go env -w GOPATH='<$HOME>/.go'
GOMODCACHE='$HOME/go/pkg/mod'
GOPATH='$HOME/go'
$ go env -w GOMODCACHE='$HOME/.go/pkg/mod'
$ go env -w GOPATH='$HOME/.go'
```

使用 `$HOME/.go` 使 go 包管理的内容像 cargo 指定的路径 `$HOME/.cargo` 一般默认在 HOME 目录不显示。然后设置 `GO111MODULE` 环境变量为 `on`,启用模块功能后 `go install` 命令使用的也是模块进行下载安装。
Expand All @@ -92,17 +100,30 @@ Output of command `go help gopath`:

> If the `GOBIN` environment variable is set, commands are installed to the directory it names instead of `DIR/bin`. `GOBIN` must be an absolute path.
VS Code 的 Go 插件需要 `gopls` 包,当然 VS Code 可以自动下载,但是和 `clangd` 类似,自己下载比较好:

```sh
$ go install golang.org/x/tools/gopls@latest
$ ln -l $GOPATH/bin/gopls /usr/local/go/bin/
```

### Upgrade

升级 go 版本的话只需下载最新的压缩包,然后根据 Install 过程更新一下二进制包即可。

### Uninstall

如果你是按照上面方法安装配置的 go,可以通过下面这条命令来进行删除:

```sh
$ sudo rm -rf /usr/local/bin/go* $HOME/.go/ $HOME/.config/go/ /path/to/go/
$ sudo rm -rf /usr/local/go/ $HOME/.go/ $HOME/.config/go/ $HOME/.cache/go-build
```

## References

- Matt Holiday\'s [Go Class](https://www.youtube.com/playlist?list=PLoILbKo9rG3skRCj37Kn5Zj803hhiuRK6)
- Go [Documentation](https://go.dev/doc/)
- Go [Playground](https://go.dev/play/#)
- Go [Packages](https://pkg.go.dev/)
- Go [Environment variables](https://pkg.go.dev/cmd/go#hdr-Environment_variables)
- Stack Overflow: [Where are the golang environment variables stored?](https://stackoverflow.com/questions/40825613/where-are-the-golang-environment-variables-stored)
- [AI Icon Generator](https://perchance.org/ai-icon-generator)
18 changes: 16 additions & 2 deletions content/posts/go/tgpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ Go 的 `switch` 语句的表达能力更加强大
> Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.
`defer` 表达式的意义为:完成函数调用前的一系列工作,但将函数调用这个动作推迟到当前函数 (`defer` 表达式所在的作用域) 结束后进行

### More types: structs, slices, and maps

Expand Down Expand Up @@ -770,7 +769,21 @@ Visit [the Go home page](https://go.dev/) for more.

{{< /admonition >}}

## Project: Building Microservices with Go
## 实作案例: Go + Fyne => Markdown Editor

`go mod init` 在创建项目时类似于 `cargo init`,而 `go get` 类似于在 cargo 项目的 `Cargo.toml` 中加入相应的依赖;可以将 `go.mod` 文件认为功能类似于 `Cargo.toml``go.sum` 文件功能类似于 `Cargo.lock``go mod tidy` 用于更新项目 (模块) 的依赖情况,使用该命令后,VS Code 的 Go 插件需要一段时间才能更新完成代码分析的缓存,不再报错。

- Stack Overflow: [Difference between go mod download and go mod tidy](https://stackoverflow.com/questions/71495619/difference-between-go-mod-download-and-go-mod-tidy)

[fyne](https://fyne.io/) 写 GUI 的思路是:使用控件 (widget) 来显示,绑定回调函数 (callback) 来设置控件动作,最后通无限循环的事件监听来实时更新图形界面。

Go 语言的模块管理和 Rust 大不相同,更偏向 C 语言的多文件拼接的方式,同一 package 的文件就好象被拼接为一个大文件来一般,无需使用 `import` 来进行引入。Go 的编译器将 `&value` 分析为取 `value` 的地址,这与 C 语言一致;但是 Go 编译器将 `&Type` 分析为类似 C++/Java `new Type {...}` 的语句,即创建 Pointer Machine 中的物件 (Object),由 GC 对这些物件进行管理。Go 语言的函数调用也遵循 C 语言的 **值传递** 哲学。至于 Go 的 Interface 与 Rust 的 Trait,主要用于编译器在编译时期对代码进行分析和检查,并不在运行时期起作用,但与 Rust 通过指定来显式实现 Trait 不同,Go 中则是通过实现全部所需的方法来隐式实现 Interface 的。Go 中的`defer` 语句可以理解为在任意 `return` 语句执行前都会被执行的语句。

Go 的测试也挺精简的: [Add a test](https://go.dev/doc/tutorial/add-a-test)

> Ending a file's name with _test.go tells the go test command that this file contains test functions.
## 实作案例: Building Microservices with Go

YouTube: Nic Jackson 制作的 [Building Microservices with Go](https://www.youtube.com/playlist?list=PLmD8u-IFdreyh6EUfevBcbiuCKzFk0EW_) 系列影片

Expand All @@ -779,6 +792,7 @@ YouTube: Nic Jackson 制作的 [Building Microservices with Go](https://www.you
## References:

- [A tour of Go](https://go.dev/tour/list)
- InkkaPlum 频道: [Go 教程](https://space.bilibili.com/290859233/channel/seriesdetail?sid=4242123)
- Nic Jackson\'s [Building Microservices with Go](https://www.youtube.com/playlist?list=PLmD8u-IFdreyh6EUfevBcbiuCKzFk0EW_): 一个不错的 Go 练习项目
- [Learn Go with Tests](https://quii.gitbook.io/learn-go-with-tests)
- [Go by Example](https://gobyexample.com/)
2 changes: 1 addition & 1 deletion content/posts/rust/demystify-rust.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Tutorial: Rust Language Demystified"
title: "Rust Language Demystified"
subtitle:
date: 2024-04-05T17:52:13+08:00
# draft: true
Expand Down
2 changes: 1 addition & 1 deletion content/posts/sysprog/c-pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,4 @@ C99 6.3.2.1 footnote
## References
- {{< link href="https://hackmd.io/@sysprog/c-pointer" content="你所不知道的 C 语言: 指针篇" external-icon=true >}}
- {{< link href="https://www.bilibili.com/video/BV1bo4y1Z7xf" content="4小时彻底掌握C指针" external-icon=true >}}
- {{< link href="https://www.bilibili.com/video/BV1bo4y1Z7xf" content="4 小时彻底掌握 C 指针" external-icon=true >}}
5 changes: 3 additions & 2 deletions content/posts/toolkit/linux-distributions.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ map <F4> : set nu!<BAR>set nonu?<CR>
- **Vim**
- **VSCode Great Icons**
- **Git History**
- **clangd**: C/C++ 语法服务 (LSP)
- **rust-analyzer**: Rust 语法服务 (LSP)
- **Go**: Go 语法服务 (LSP)
- **Even Better TOML**: toml 语法高亮
- **clangd**: C/C++ 语法服务
- **Native Debug**: 调试 C/C++
- **rust-analyzer**: Rust 语法服务
- **CodeLLDB**: 调试 C++/Rust
- **Dependi**: 管理包依赖关系
- **Error Lens**: 更强大的错误提示
Expand Down

0 comments on commit 65f3073

Please sign in to comment.