dszn2485 2018-04-28 18:30
浏览 16
已采纳

从另一个包导出的功能

I am following instructions in https://golang.org/doc/code.html#Workspaces link and I build my first Go program.

So, I tried to make library with this instruction = https://golang.org/doc/code.html#Library

and everything is perfect until building hello.go, its gives me this error.

/hello.go:10:13: undefined: stringutil.Reverse

I've already rebuild my reverse.go.

Thats my code:

package main

import (
    "fmt"

    "github.com/d35k/stringutil"
)

func main() {
    fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}

that's my reverse.go (same as docs)

package stringutil

func reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
}

and my gopath variable

export GOPATH=$HOME/GoLang

and my files ar in

GoLang/src/github.com/mygithubusername/
  • 写回答

1条回答 默认 最新

  • dpb_4431 2018-04-28 18:54
    关注

    Golang Tour specify exported name as

    A name is exported if it begins with a capital letter. And When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.

    Change the name of reverse func to Reverse to make it exportable to main package. Like below

    package stringutil
    
    func Reverse(s string) string {
        r := []rune(s)
        for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
            r[i], r[j] = r[j], r[i]
        }
        return string(r)
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动