中断多行函数调用

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

Break multi line function call

问题

我正在为Go语言编写一个MySQL库/包装器。对于我的API的使用,我想象了以下的形式:

model
	.Select("INTERNAL_ID")
	.Select("EXTERNAL_ID")
	.Find(1);

例如,以下是一些函数:

func (model SQLModel) Select(selectString string) SQLModel 
func (model SQLModel) Find(id int) interface{}

问题是,我无法将函数调用分成多行,因为go/fmt会报错:在每个换行处出现syntax error: unexpected ., expecting }

现在,我可以这样写:

model.Select("INTERNAL_ID").Select("EXTERNAL_ID").Find(1)

但是随着API的增长(例如where、join、sum等),阅读起来会变得困难。

那么,我该如何在Go中拆分函数调用呢?

谢谢。

英文:

I am working on a library / wrapper for mysql in go. For usage of my API I imagined something like:

model
	.Select("INTERNAL_ID")
	.Select("EXTERNAL_ID")
	.Find(1);

With, as example, the following functions:

func (model SQLModel) Select(selectString string) SQLModel 
func (model SQLModel) Find(id int) interface{}

The problem is that I can't break my function calls into multiple lines as go/fmt complains: syntax error: unexpected ., expecting } at every break.

Now, I could go :

model.Select("INTERNAL_ID").Select("EXTERNAL_ID").Find(1)

But as the API grows (where, join, sum, etc, ...) it'll rapidly become hard to read.

So, how can I split function calls in GO ?

Thanks,

答案1

得分: 12

点号必须位于行尾:

model.
    Select("INTERNAL_ID").
    Select("EXTERNAL_ID").
    Find(1)
英文:

The dots must be at the end of line:

model.
    Select("INTERNAL_ID").
    Select("EXTERNAL_ID").
    Find(1)

huangapple
  • 本文由 发表于 2017年3月28日 00:15:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/43051496.html
匿名

发表评论

匿名网友

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

确定