我正在尝试使用go的模板系统编写一个看起来像这样的字符串:(p1,p2,p3),其中p1,p2,...来自程序中的数组.我的问题是如何正确地为最后(或第一个)元素放置逗号.
我输出的非工作版本(p1,p2,p3,)看起来像这样:
package main
import "text/template"
import "os"
func main() {
ip := []string{"p1", "p2", "p3"}
temp := template.New("myTemplate")
temp,_ = temp.Parse(paramList)
temp.Execute(os.Stdout, ip)
}
const paramList =
`{{ $i := . }}({{ range $i }}{{ . }}, {{end}})`
Run Code Online (Sandbox Code Playgroud)
到目前为止,我最好的线索可以在http://golang.org/pkg/text/template/中找到以下声明:
如果"范围"操作初始化变量,则将变量设置为迭代的连续元素.另外,"范围"可以声明两个变量,用逗号分隔:
$index, $element := pipeline
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$ index和$ element分别设置为数组/切片索引或映射键和元素的连续值.请注意,如果只有一个变量,则为其分配元素; 这与Go range子句中的约定相反.在哪里建议索引
这表明可以在迭代中获取索引,但我无法弄清楚声明两个变量的范围是什么意思,以及模板中应该声明这些变量的位置.
我很想知道您清理公布的头文件有哪些例程可以分发给客户.
我想听听你的意见的一些事情是:
评论不适用于外部消费.一般来说,我喜欢保持文档接近代码,这样的评论可能不是一个好主意分享:
/**
* @todo Should we change the signature of this function to
* make it obvious that xxx is really yyy?
*/
Run Code Online (Sandbox Code Playgroud)
也许:
/**
* @todo Add support for feature X
*/
Run Code Online (Sandbox Code Playgroud)
选项卡样式不一致:
void functionA(int a,
int b,
int c,
int d);
void functionB(int a,
int b,
int c);
Run Code Online (Sandbox Code Playgroud)
是否有任何工具可用于准备标题或代码以供发布?
我有一堆自动生成的LaTeX代码,其格式为"functionname_2093840289fad1337",即附加了散列的函数的名称.我想通过仅引用我知道的唯一函数名来从文档的其余部分引用这些函数.我想要一个像这样的查找函数:
\hyperdyperlink{functionname}
Run Code Online (Sandbox Code Playgroud)
发出的
\hyperlink{functionname_2093840289fad1337}{functionname}
Run Code Online (Sandbox Code Playgroud)
请注意,我无法计算哈希,但我准备编写一个表,将每个函数名映射到functionname + hash.编写这种函数的最佳方法是什么?
我正在为一个使用SWIG公开其C++ API的程序编写python脚本.SWIG公开的函数有如下界面:
void writePixelsRect(JoxColor* colors, int left, int top, int width, int height);
Run Code Online (Sandbox Code Playgroud)
JoxColor是一个POD结构,如下所示:
struct JoxColor {
float r, g, b, a;
};
Run Code Online (Sandbox Code Playgroud)
我可以在Python中轻松创建一个JoxColor并调用writePixelsRect,如下所示:
c = JoxApi.JoxColor()
c.r = r
c.g = g
c.b = b
c.a = a
JoxApi.writePixelsRect(c, x, y, 1, 1)
Run Code Online (Sandbox Code Playgroud)
重复调用带有1x1像素矩形的writePixelsRect是非常慢的,所以我想从python创建一个JoxColor数组,这样我就可以编写更大的矩形了.SWIG类型可以实现这一点吗?
请注意,我无法访问公开JoxColor和writePixelsRect的C++库的源代码,因此我无法为此添加帮助功能.我也不想在系统中引入新的C++代码,因为它会强制我的python脚本的用户在他们运行的任何平台上编译C++代码.我可以在python环境中访问ctypes,所以如果我能以某种方式将在ctypes中创建的float数组类型转换为适用于SWIG的JoxColor*类型,它对我有用.