drd0833 2017-03-14 02:45 采纳率: 100%
浏览 3
已采纳

是否有将选项传递给Go函数的规范方法?

I am writing a package that exposes this function:

func Marshal(input interface{}) ([]byte, error)

This is fine for the majority of situations, but I also want to prove another function if there are extra options:

type MarshalOptions struct {
    OnlyStdClass bool
}

My first thought is to create another function:

func MarshalWithOptions(input interface{}, options MarshalOptions) ([]byte, error)

Is this the recommended way of doing this? Is there a standard naming convention for function that also provide a more specific version with options?

  • 写回答

3条回答 默认 最新

  • dongyisa6254 2017-03-14 04:58
    关注

    You can take a *MarshalOptions. The caller can then pass nil if they want the default behavior.

    eg. func Marshal(input interface{}, options *MarshalOptions) ([]byte, error)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 python密码表示
  • ¥15 Flask应用部署到阿里云服务器失败,无法访问网页,问题出在哪里?
  • ¥15 计算个体的IBS遗传距离
  • ¥15 有什么好的直流步进减速电机42的厂家推荐,保持力矩达到0.3N.M
  • ¥15 一道蓝桥杯的题,请问我错在哪里
  • ¥15 关于#android问题,xposed模块找不到so
  • ¥15 UE5.4.2创建C++项目成功,但是编译失败
  • ¥15 华为手机备份nas,airdisk,遇到的空间不足问题。求个详细的解决方案。
  • ¥20 Cknife无法使用
  • ¥15 这个结构体为什么会出错呢?