Error in importing custom packages in Go Lang

前端 未结 3 387
有刺的猬
有刺的猬 2021-02-01 13:11

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

src
|-libfastget
|  |-libfastget.go
|
|-MainPro         


        
3条回答
  •  萌比男神i
    2021-02-01 13:39

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

提交回复
热议问题