修改值/结构的最佳实践方法

huangapple go评论118阅读模式
英文:

Best practice to modify value/struct

问题

我发现修改值/结构的方法有两种(可能还有其他方法)。

  1. 通过引用传递值
  2. 从函数/方法获取值,处理错误,然后赋值

我知道这两种方法都可以工作,但我想知道在Go语言世界中哪种方法是最佳实践。

我在这里给出了两个示例。

abc.go

package main

import (
	"fmt"
	"github.com/pkg/errors"
)

type Request struct {
	AdType string
}

type Response struct {
	Message string
}

func main() {
	// 实现方法1
	// 通过处理函数来修改响应
	res1 := Response{}
	req1 := Request{AdType: "banner"}
	req1.handleResponse(&res1)

	// 实现方法2
	res2 := Response{}
	req2 := Request{AdType: "image"}
	if r, e := req2.getRequestMessage(); e == nil {
		res2.Message = r
	}
	fmt.Println(res1)
	fmt.Println(res2)
}

func (req *Request) handleResponse(res *Response) {
	if req.AdType == "banner" {
		res.Message = "Banner Ad"
	} else if req.AdType == "video" {
		res.Message = "Video Ad"
	}
}

func (req *Request) getRequestMessage() (string, error) {
	if req.AdType == "banner" {
		return "Banner Ad", nil
	} else if req.AdType == "video" {
		return "Video Ad", nil
	}
	return "", errors.New("Invalid ad type")
}
英文:

I found that to modify a value/struct I can do it in two ways(maybe others exist).

  1. Pass value by reference
  2. Get value from function/method, handle error then assign

I know both methods work but I am wondering which method is best practice in the golang world.

I am giving both examples here.

abc.go

package main

import (
	"fmt"
	"github.com/pkg/errors"
)

type Request struct {
	AdType string
}

type Response struct {
	Message string
}

func main() {
	// Implementation 1
	// Handle response by handle and alter response
	res1 := Response{}
	req1 := Request{AdType: "banner"}
	req1.handleResponse(&res1)

	// Implementation 2
	res2 := Response{}
	req2 := Request{AdType: "image"}
	if r, e := req2.getRequestMessage(); e == nil {
		res2.Message = r
	}
	fmt.Println(res1)
	fmt.Println(res2)
}

func (req *Request) handleResponse(res *Response) {
	if req.AdType == "banner" {
		res.Message = "Banner Ad"
	} else if req.AdType == "video"{
		res.Message = "Video Ad"
	}
}

func (req *Request) getRequestMessage() (string, error) {
	if req.AdType == "banner" {
		return "Banner Ad", nil
	} else if req.AdType == "video"{
		return "Video Ad", nil
	}
	return "", errors.New("Invalid ad type")
}

答案1

得分: 1

老实说,我认为像这样的代码更符合Go的标准。它更清晰、更易于理解。

package main

import (
	"errors"
	"fmt"
)

type Request struct {
	AdType string
}

type Response struct {
	Message string
}

func main() {
	res, err := handleRequest(&Request{AdType: "banner"})
	if err != nil {
		fmt.Println("获取响应时出错")
	}
	fmt.Println(res)
}

func handleRequest(request *Request) (*Response, error) {
	res := new(Response)
	switch request.AdType {
	case "banner":
		res.Message = "横幅广告"
		return res, nil
	case "video":
		res.Message = "视频广告"
		return res, nil
	}
	return nil, errors.New("无效的广告类型")
}
英文:

Honestly, I think something like this is more of the standard in go. It's a lot cleaner and easier to follow.

package main

import (
	"errors"
	"fmt"
)

type Request struct {
	AdType string
}

type Response struct {
	Message string
}

func main() {
	res, err := handleRequest(&Request{AdType: "banner"})
	if err != nil {
		fmt.Println("error getting response")
	}
	fmt.Println(res)
}

func handleRequest(request *Request) (*Response, error) {
	res := new(Response)
    switch request.AdType {
	case "banner":
		res.Message = "Banner Ad"
		return res, nil
	case "video":
		res.Message = "Video Ad"
		return res, nil
	}
    return nil, errors.New("invalid ad type")
}

huangapple
  • 本文由 发表于 2021年11月18日 15:33:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70016027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定