Files
knowledge-base/语言/Java/Golang.md
T
2026-08-25 22:44:19 +08:00

200 lines
4.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Golang
> 来源:原 mongodb 集合 `t_blog` · 整理日期:2026-08-25
> 分类 ID:`2023022315480800000000000001` · 排序:12 · 文章数:4
## 目录
1. [3. grpc xds](#3-grpc-xds)
2. [2. grpc health check](#2-grpc-health-check)
3. [1. grpc protoc](#1-grpc-protoc)
4. [4. Go](#4-go)
---
## 1. 3. grpc xds
<sub>bid: `2023022315480900000000000020`</sub>
该写点什么么...
---
## 2. 2. grpc health check
<sub>bid: `2023022315484500000000000019`</sub>
##
---
## 3. 1. grpc protoc
<sub>bid: `2023022315507600000000000021`</sub>
## base consepts
- protobuf
我们可以查看官方文档 [protobuf guides](https://protobuf.dev/programming-guides/proto3/)
简单来说就是一种以 .proto 结尾文件,遵循protocol buffer language 语法,其中包含一个或者多个message和service。
**notice_service.proto**
syntax = "proto3";
package notice.service.v1;
option go_package ="violin-notice/pkg/service/notice.service.v1";
message NoticeMessage {
};
message NoticeResponse {};
service NoticeService {
rpc SendNotice(NoticeMessage) returns (NoticeResponse) {}
}
- protoc
[protoc guides](https://github.com/bufbuild/protoc-gen-validate)
作为可执行程序,其作用是将上面的我们定义的.proto文件转换成对应语言的grpc 可用的文件,我们通过对这些文件进行扩展来实现我们想要的场景。
protoc --go_out=./gen --go_opt=paths=source_relative --go-grpc_out=./gen --go-grpc_opt=paths=source_relative notice_service.proto
执行上面命令会生成go语言的两个文件
notice_service_grpc.pb.go
notice_service.pb.go
我们只需要自己写一个notice_service.go 来扩展上述两个文件即可。
**package notice_service_v1**
import (
"context"
"gopkg.in/gomail.v2"
"log"
"violin-home.cn/common/logs"
)
type NoticeService struct {
UnimplementedNoticeServiceServer
}
func New() *NoticeService {
return &NoticeService{}
}
func (*NoticeService) SendNotice(
ctx context.Context, msg *NoticeMessage) (*NoticeResponse, error) {
// to write logic
return &NoticeResponse{}, nil
}
## 如何使用grpc
grpc 存在client和server
---
## 4. 4. Go
<sub>bid: `2023030213489100000000000022`</sub>
## 1. 概述
一些简单的概念介绍可以通过下面网站学习到。
[golang 菜鸟教程](https://www.runoob.com/go/go-tutorial.html)
## 2. package
golang的包管理模式是树形的,并且有以下规约:
**1**
树形结构的根节点是main,即项目根目录。
**2**
文件中的包定义 一定是和该文件所处的文件夹相同
**3**
文件中定义的函数,如果是像作为第三方包引用,函数的首字母必须大写
# violin-controller 工程为例 https://github.com/simple321vip/violin-controller
ls
main.go controller.go controller_test.go 都是 根目录下的文件,所以他们的package 都是 main
请注意 包 的名字只能是该文件夹的名字,并不包括父级路径
**4 引包**
引包 包括第三方包 和 项目自定义包
在mod模式下,第三方包存在 \$GOPATH/pkg/mod
而自定义包 引入的时候直接从采用 项目相对路径的方式引入
## 3. 变量与指针
变量分为 值变量和指针变量
## 4. 函数
**1. main()** 函数
程序入口函数
**2. init()** 函数
每一个源文件都可以包含一个init函数,init意为初始化,也表明init函数会在main函数执行之前执行。
**3. go func()** 函数
并发编程 即异步
**4. func() {}()** 匿名函数
匿名函数可以通过变量指向匿名函数,可以匿名函数直接执行
// 声明一个函数变量
f := func() int {
return 1
}
// 调用函数
f()
// 也可以直接执行匿名函数
func() int {
return 1
}()
**5. func sample() func() int {}** 通过匿名函数实现闭包
package main
import "fmt"
func getSequence() func() int {
i:=0
return func() int {
i+=1
return i
}
}
func main(){
/* nextNumber 为一个函数,函数 i 为 0 */
nextNumber := getSequence()
/* 调用 nextNumber 函数,i 变量自增 1 并返回 */
fmt.Println(nextNumber())
fmt.Println(nextNumber())
fmt.Println(nextNumber())
/* 创建新的函数 nextNumber1,并查看结果 */
nextNumber1 := getSequence()
fmt.Println(nextNumber1())
fmt.Println(nextNumber1())
}
---