Golang:函数返回参数错误

时间:2017-05-10 15:26:45

标签: go

以下代码给出:

runtime.main: call to external function main.main
runtime.main: main.main: not defined
runtime.main: undefined: main.main

我搞砸了返回的论点,但为什么呢?

请求:

fmt.Println( reflect.TypeOf(l)) 

将* ldap.Conn作为类型

代码可以在不尝试返回对象的情况下工作

package main

import (
    "fmt"
    "log"

    "gopkg.in/ldap.v2"
)


var Ldap1 = "10.0.0.1"
var Lport1 = 389
var Prpl1 = "cn=admin,dc=Example,dc=com"
var Pass1 = "password"

func Bindldap(ldaphost string, port int, principal string, password string) *ldap.Conn {

    l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldaphost, port))
    if err != nil {
        log.Fatal(err)
    }
    defer l.Close()

    err = l.Bind(principal, password)
    if err != nil {
        log.Fatal(err)
    }
    return l
}

func Main() {
    a := Bindldap(Ldap1, Lport1, Prpl1, Pass1)
    //do something with a
}

1 个答案:

答案 0 :(得分:2)

您的错误消息告诉您它正在main包中寻找名为main的功能。您调用了入口点Main(注意上限)。试试这个:

func main() {
    a := Bindldap(Ldap1, Lport1, Prpl1, Pass1)
    //do something with a
}