我正在将一个C库移植到Go.AC函数(使用varargs)定义如下:
curl_easy_setopt(CURL *curl, CURLoption option, ...);
Run Code Online (Sandbox Code Playgroud)
所以我创建了包装器C函数:
curl_wrapper_easy_setopt_str(CURL *curl, CURLoption option, char* param);
curl_wrapper_easy_setopt_long(CURL *curl, CURLoption option, long param);
Run Code Online (Sandbox Code Playgroud)
如果我在Go中定义函数如下:
func (e *Easy)SetOption(option Option, param string) {
e.code = Code(C.curl_wrapper_easy_setopt_str(e.curl, C.CURLoption(option), C.CString(param)))
}
func (e *Easy)SetOption(option Option, param long) {
e.code = Code(C.curl_wrapper_easy_setopt_long(e.curl, C.CURLoption(option), C.long(param)))
}
Run Code Online (Sandbox Code Playgroud)
Go编译器抱怨:
*Easy·SetOption redeclared in this block
Run Code Online (Sandbox Code Playgroud)
Go支持函数(方法)重载也是如此,或者此错误是否意味着其他内容?
有没有办法在Go的函数中指定默认值?我试图在文档中找到这个,但我找不到任何指定这甚至可能的东西.
func SaySomething(i string = "Hello")(string){
...
}
Run Code Online (Sandbox Code Playgroud) 我可以在Go的源代码中找到它们的实现make.
事实证明,"代码搜索"功能对于语言的这种中心功能几乎没用,我没有办法确定是否应该搜索C函数,Go函数或者什么.
同样在将来我如何在不诉诸此处的情况下弄清楚这种事情?(即:教我钓鱼)
编辑 PS我已经找到http://golang.org/pkg/builtin/#make,但是,与其他go包不同,它不包含指向源的链接,可能是因为它位于编译器的深处.
有没有办法在 Go 编程语言中将参数声明为“可选”?
我的意思的例子:
func doSomething(foo string, bar int) bool {
//...
}
Run Code Online (Sandbox Code Playgroud)
我希望该参数bar是可选的,并且0如果没有传递任何内容,则默认为该参数。
doSomething("foo")
Run Code Online (Sandbox Code Playgroud)
会是一样的
doSomething("foo",0)
Run Code Online (Sandbox Code Playgroud)
我在有关 Functions 的官方文档中找不到有关此事的任何信息。
让我们说,我min()(例如)我有一个可变函数来定义所提供的多个值的最小值.
如果调用者没有提供任何参数,我想暂停编译过程(因为这将是调用者中的错误,而不是我的函数中的错误).
怎么做?
我想知道是否有一种方法可以像在Java中那样在Go中实现多个构造函数(具有相同的函数名)。另一个选择可能是只有一个带有可选参数的构造函数,但是我不确定如何准确地做到这一点。
type Query struct {
TagsQuery string
PageQuery string
}
// First Constructor
func NewQuery(TagsQuery string) Query {
return Query{
TagsQuery: TagsQuery,
PageQuery: "0", // default to first page
}
}
// Second Constructor
func NewQuery(TagsQuery string, PageQuery string) Query {
return Query{
TagsQuery: TagsQuery,
PageQuery: PageQuery,
}
}
Run Code Online (Sandbox Code Playgroud)
第一个构造函数采用一个参数TagsQuery,默认PageQuery为0。第二个构造函数采用两个参数:TagsQuery和PageQuery。
我有一个当前没有收到bool参数的函数,但随后用硬编码的bool调用另一个函数.我们需要删除硬编码调用并允许传递bool.
我首先想到我可以尝试一些默认参数 - 我的谷歌搜索导致Go显然不支持可选(resp.default)参数.
所以我想我会尝试函数重载.
我在reddit上发现了这个帖子,它表示它可以使用特殊指令,因为版本1.7.3:https:
//www.reddit.com/r/golang/comments/5c57kg/when_did_function_overloading_get_slipped_in/
我正在使用1.8,但我仍然无法获得它工作.
我甚至不确定我是否可以被允许使用该指令,但我猜测立即更改功能签名可能很危险,因为我不知道谁使用该功能......
无论如何 - 即使// +重载它也没有用
Go中是否有任何"特殊"方式或模式来解决这个问题?
//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string, dynamic bool) error {
result, err := anotherFunc(rpath, dynamic)
}
//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string ) error {
//in this function anotherFunc was being called, and dynamic is hardcoded to true
//result, err := anotherFunc(rpath, true)
return self.Apply(rpath, lpath, true)
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的测试时,我得到(原谅我省略了部分真正的文件路径):
too many arguments …Run Code Online (Sandbox Code Playgroud) 我读过两个可选参数?和Golang传递nil作为函数的可选参数?
并且仍然想知道我的案例是否更具体.
我得到的是:
type Periodical struct {
Interval *interval.Interval
StartsAt time.Time
EndsAt time.Time
}
Run Code Online (Sandbox Code Playgroud)
表示具有开始日期并且可能有也可能没有结束日期的期刊事件(期刊事件无限期地运行).
eachYear := Periodical{
interval.Years(1),
StartsAt: time.Parse("2 Jan 2006", "1 Jan 1970")}
Run Code Online (Sandbox Code Playgroud)
会抛出
periodical/periodical.go:31:39: cannot use nil as type time.Time in field value
Run Code Online (Sandbox Code Playgroud)
据了解, - 我没有说明EndsAt time.Time.
但那我到底做了什么呢?
我被迫有一个特殊的旗帜可以忽略EndsAt这样吗?
type Periodical struct {
Interval *interval.Interval
StartsAt time.Time
EndsAt time.Time
isIndefinite bool // This looks ugly already
}
Run Code Online (Sandbox Code Playgroud)
然后,如果我想Yearly/ Anually我做的事情
eachYear := Periodical{
interval.Years(1),
time.Parse("2 …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据Golang 和 default values帖子的选项 1 实现默认值。但是当我尝试go install在终端中执行以下错误时:
not enough arguments in call to test.Concat1
have ()
want (string)
Run Code Online (Sandbox Code Playgroud)
代码:
package test
func Concat1(a string) string {
if a == "" {
a = "default-a"
}
return fmt.Sprintf("%s", a)
}
Run Code Online (Sandbox Code Playgroud)
// 其他包
package main
func main() {
test.Concat1()
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。