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

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

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,
}

p, err := proto.Marshal(pb)
if err != nil {
    return -1, err
}

return w.Write(p)

}

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

英文:
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,
	}

	p, err := proto.Marshal(pb)
	if err != nil {
		return -1, err
	}

	return w.Write(p)
}

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结构来调用这个方法:

r := &AppendEntriesRequest{}
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:

r := &AppendEntriesRequest{}
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:

确定