dongtu9823
2017-05-10 15:26
采纳率: 0%
浏览 81
已采纳

Golang:函数返回参数错误

The following code gives:

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

I messed up with the return argument, but why?

Requesting:

fmt.Println( reflect.TypeOf(l)) 

gives *ldap.Conn as type

The code works without trying to return the object

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
}

图片转代码服务由CSDN问答提供 功能建议

以下代码给出:

  runtime.main:调用 到外部函数main.main 
runtime.main:main.main:未定义
runtime.main:未定义:main.main 
   
 
 

我搞砸了返回 参数,但为什么呢?

请求:

  fmt.Println(reflect.TypeOf(l))
  <  / pre> 
 
 

将* ldap.Conn设置为类型

该代码无需尝试返回对象即可工作

 <代码 >软件包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字符串,端口int,主体字符串,密码字符串)* ldap.Conn {\  n 
l,err:= ldap.Dial(“ tcp”,fmt.Sprintf(“%s:%d”,ldaphost,port))
如果err!= nil {
 log.Fatal(err)
  } 
延迟l.Close()
 
 err = l.Bind(主密码)
如果err!= nil {
 log.Fatal(err)
  } 
 return l 
} 
 
func Main(){
a:= Bindldap(Ldap1,Lport1,Prpl1,Pass1)
 //使用a 
} 
    > 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dousilie9522 2017-05-10 15:34
    最佳回答

    You error messages are telling you it's looking for a function called main in your main package. You called your entry point Main (Note the caps). Try this:

    func main() {
        a := Bindldap(Ldap1, Lport1, Prpl1, Pass1)
        //do something with a
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题