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.
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.and this is my
sayHello.go
file, please note to be able to export a function thefunc
name should start with a CapitalCase SayHelloand now I am able to import utils package into main.go file
you would need to make your function exportable with an uppercase for its name:
Used as:
The spec mentions: "Exported identifiers":
Lu
"); and