go-zero开发日记

35 阅读2分钟

1. 解决WSL2占用内存过多问题(Docker on WSL2: VmmemWSL)

2.1 创建.wslconfig文件

路径位置C:\Users\<UserName>\.wslconfig,如果找不到也可以在Win+R,然后输入%userprofile%回车即可

如果目录下不存在,则创建.wslconfig(多数情况下,是不存在的)

文件内容如下:

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 2 GB, this can be set as whole numbers using GB or MB
memory=2GB 

# Sets the VM to use two virtual processors
processors=6

# Sets amount of swap storage space to 2GB, default is 25% of available RAM
swap=2GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=C:\\temp\\wsl-swap.vhdx

2.2 重启wsl2

# 关闭wsl
wsl --shutdown
# 启动wsl
wsl

  1. 指针易错点:返回值若是指针一定要new

  2. 连表的时候可以参考的resp

    1. API层

      1. type (
            LotteryDetailReq {
                Id int64 `json:"id"`
            }
            LotteryDetailResp {
                Id int64 `json:"id"`
                Name string `json:"name"`
                UserId int64 `json:"userId"`                                   //发起抽奖用户ID
                PublishType int64 `json:"publishType"`                         //开奖设置:1按时间开奖 2按人数开奖 3即抽即中
                IsSelected int64 `json:"isSelected"`                           //是否精选 1是 0否
                PublishTime int64 `json:"publish_time"`                        //开奖时间
                JoinNumber int64 `json:"join_number"`                          //自动开奖人数标准
                Introduce string `json:"introduce"`                            //抽奖说明
                AwardDeadline int64 `json:"awardDeadline"`                     //领奖截止时间
                Prizes []*CreatePrize `json:"prizes"`                           //奖品信息
            }
        )
        
    2. API logic

      1. func (l *LotteryDetailLogic) LotteryDetail(req *types.LotteryDetailReq) (resp *types.LotteryDetailResp, err error) {
        	res, err := l.svcCtx.LotteryRpc.LotteryDetail(l.ctx, &lottery.LotteryDetailReq{
        		Id: req.Id,
        	})
        	if err != nil {
        		//todo 要使用这种写法管理错误,否则Kibana无法收集到错误日志的详情
        		return nil, errors.Wrapf(xerr.NewErrMsg("Failed to get LotteryDetail"), "Failed to get SearchLottery err : %v ,req:%+v", err, req)
        	}
        	resp = new(types.LotteryDetailResp)
        	_ = copier.Copy(resp, res)
        	_ = copier.Copy(resp, res.Lottery)
        	return
        }
        
    3. RPC层(protoc):

      1. message LotteryDetailReq {
          int64 Id = 1; // 抽奖id
        }
        
        message LotteryDetailResp {
          Lottery lottery = 1; // 抽奖信息
          repeated Prize prizes = 2; // 奖品列表
        }
        
    4. RPC logic

      1. func (l *LotteryDetailLogic) LotteryDetail(req *types.LotteryDetailReq) (resp *types.LotteryDetailResp, err error) {
        	res, err := l.svcCtx.LotteryRpc.LotteryDetail(l.ctx, &lottery.LotteryDetailReq{
        		Id: req.Id,
        	})
        	if err != nil {
        		//todo 要使用这种写法管理错误,否则Kibana无法收集到错误日志的详情
        		return nil, errors.Wrapf(xerr.NewErrMsg("Failed to get LotteryDetail"), "Failed to get SearchLottery err : %v ,req:%+v", err, req)
        	}
        	resp = new(types.LotteryDetailResp)
        	_ = copier.Copy(resp, res)
        	_ = copier.Copy(resp, res.Lottery)
        	return
        }
        

      4. 返回值经验:如果只有一层返回结果,就不用再包一层了

      LotteryDetailResp {
              Id int64 `json:"id"`
              Name string `json:"name"`
              UserId int64 `json:"userId"`                                   //发起抽奖用户ID
              PublishType int64 `json:"publishType"`                         //开奖设置:1按时间开奖 2按人数开奖 3即抽即中
              IsSelected int64 `json:"isSelected"`                           //是否精选 1是 0否
              PublishTime int64 `json:"publish_time"`                        //开奖时间
              JoinNumber int64 `json:"join_number"`                          //自动开奖人数标准
              Introduce string `json:"introduce"`                            //抽奖说明
              AwardDeadline int64 `json:"awardDeadline"`                     //领奖截止时间
              Prizes []*CreatePrize `json:"prizes"`                           //奖品信息
          }
          // 不用再包一层lotteryDetail了