英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论