go函数输入,func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {

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

go function input, func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {

问题

func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {
pb := &protobuf.AppendEntriesRequest{
Term: proto.Uint64(req.Term),
PrevLogIndex: proto.Uint64(req.PrevLogIndex),
PrevLogTerm: proto.Uint64(req.PrevLogTerm),
CommitIndex: proto.Uint64(req.CommitIndex),
LeaderName: proto.String(req.LeaderName),
Entries: req.Entries,
}

  1. p, err := proto.Marshal(pb)
  2. if err != nil {
  3. return -1, err
  4. }
  5. return w.Write(p)

}

对于这个函数,"w" 是输入吗?req又是什么?有点困惑。
谢谢

英文:
  1. func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {
  2. pb := &protobuf.AppendEntriesRequest{
  3. Term: proto.Uint64(req.Term),
  4. PrevLogIndex: proto.Uint64(req.PrevLogIndex),
  5. PrevLogTerm: proto.Uint64(req.PrevLogTerm),
  6. CommitIndex: proto.Uint64(req.CommitIndex),
  7. LeaderName: proto.String(req.LeaderName),
  8. Entries: req.Entries,
  9. }
  10. p, err := proto.Marshal(pb)
  11. if err != nil {
  12. return -1, err
  13. }
  14. return w.Write(p)
  15. }

For this function, is "w" input? what about req? Kinda confused here.
Thanks

答案1

得分: 1

这是一个Go方法。

AppendEntriesRequest是一个类型,req *AppendEntriesRequest是该类型的指针。
在其他语言中,你可以将reqthisself进行比较。

w io.Writer是该函数的输入。

(int, error)是返回值。

你可以通过实例化一个AppendEntriesRequest结构来调用这个方法:

  1. r := &AppendEntriesRequest{}
  2. n, err := r.Encode(...)
英文:

that is a Go Method

AppendEntriesRequest is a type and req *AppendEntriesRequest is a pointer to that type.
You can compare req in other languages as this or self

w io.Writer is the input of the function.

(int, error) are the return values.

You can invoke this method by instantiating a AppendEntriesRequest structure:

  1. r := &AppendEntriesRequest{}
  2. n, err := r.Encode(...)

huangapple
  • 本文由 发表于 2014年8月26日 09:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/25496845.html
匿名

发表评论

匿名网友

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

确定