Error in importing custom packages in Go Lang

﹥>﹥吖頭↗ 提交于 2019-12-02 20:09:21

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.

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

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()
}

  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"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!