55

I have created a library by the name libfastget which is in the src with my program as

src
|-libfastget
|  |-libfastget.go
|
|-MainProgram
   |-main.go

and the libfastget exports a funtion fastget as follows

package libfastget

import (
    "fmt"
    "io"

)


func fastget(urlPtr *string, nPtr *int, outFilePtr *string) download {
    .....
    return dl

}

When I use the library in my main program

package main

import (
    "fmt"
    "net/http"
    "os"
    "libfastget"
    "path/filepath"
    "strings"
    "flag"
    "time"

)
func uploadFunc(w http.ResponseWriter, r *http.Request) {

         n:=libfastget.fastget(url,4,filename)

    }

}

I get the following error upon trying to build with go build

# FServe
./main.go:94: cannot refer to unexported name libfastget.fastget
./main.go:94: undefined: libfastget.fastget

The strange thing is that the library file libfastget.a is present in the pkg folder.

peterh
  • 11,875
  • 18
  • 85
  • 108
Shenal Silva
  • 1,955
  • 5
  • 26
  • 37
  • 3
    maybe duplicate of http://stackoverflow.com/questions/24487943/invoke-golang-struct-function-gives-cannot-refer-to-unexported-field-or-method . you must capitalize the function name to export it. – ymonad Aug 26 '14 at 09:06

4 Answers4

142

you would need to make your function exportable with an uppercase for its name:

func Fastget(...

Used as:

n:=libfastget.Fastget(url,4,filename)

The spec mentions: "Exported identifiers":

An identifier may be exported to permit access to it from another package. An identifier is exported if both:

  • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
  • the identifier is declared in the package block or it is a field name or method name.

All other identifiers are not exported.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I just realized that the reffernce was pointing to my source code instead of the compiled library How can I fix that ?? – Shenal Silva Aug 26 '14 at 09:21
  • @ShenalSilva once it compiles fully, your compiled library will be in sync with your source code. – VonC Aug 26 '14 at 09:22
  • define fully compiled ?? I compiled my library using go install libfastget – Shenal Silva Aug 26 '14 at 09:36
  • `cd libfastget` ; `go build`, then `cd MainProgram`; `go install`. You don't "install" your lib, only your main program: it will be statically linked with all its dependencies (including `libfastget`) and will generate one executable in `GOPATH/bin`. – VonC Aug 26 '14 at 09:37
  • what is the use of the libfastget.a file in the pkg directory if my main program does not build after i delete the source of the library package – Shenal Silva Aug 26 '14 at 10:02
  • 1
    @ShenalSilva you need the source of the library package in order to compile your main program. If those sources are *not* more recent than the `libfastget.a`, then they won't be recompiled. But you need those sources. – VonC Aug 26 '14 at 10:33
  • omg! "uppercase" ... remake all function kkk... good idea, Thanks VonC – KingRider Jun 02 '16 at 12:13
3

to export a function into another package the function identifier must start with a capital letter.

0

I recently started learning GO Lang (2 days back) And what I found was you need to setup a workspace folder to make the local packages import into other projects or main.go files. I'm using VS Code editor. Please correct me if Im wrong, but this setup works fine for me.

Inside your bash_profile OR .zshrc file add below lines, update the GOPATH as per your folder path.

export GOPATH=~/projects/GO_PROJECTS
export PATH=$PATH:$GOPATH/bin:$PATH

enter image description here

and this is my sayHello.go file, please note to be able to export a function the func name should start with a CapitalCase SayHello

package utils

import "fmt"

func SayHello() {
    fmt.Println("Hello, Ajinkya")
}

and now I am able to import utils package into main.go file

package main

import (
    "go_proj1/utils"
)

func main() {
    utils.SayHello()
}
STEEL
  • 8,955
  • 9
  • 67
  • 89
  • Its not standard procedure to name package as *utils* according to [GO_Doc](https://blog.golang.org/package-names#TOC_5). – Yogi Aug 10 '18 at 14:01
-1

  1. set the current directory as GOPATH
  2. or you can use local import as follows


    move your main.go to the ../ directory to the libfastget.go.
    i mean the files looks like:
    src
    |-libfastget
    | |-libfastget.go
    |
    |-main.go

import "./libfastget"
JessonChan
  • 216
  • 2
  • 3