call a function in a differnet file golang code example

Example: golang import function from file

////////////////////////////////////
//This is the code inside test1.go//
////////////////////////////////////

package main

import (
    L "./lib" // This will import ALL the files inside the lib folder
)

func main() {
    L.Demo()
}

///////////////////////////////////////////////////////////////////////////
//This is the code inside test2.go, this is inside a subfolder called lib//
///////////////////////////////////////////////////////////////////////////

package lib // All the files inside lib will have the same package name

import "fmt"

// This func must be Exported, Capitalized, and comment added.
func Demo() {
    fmt.Println("HI")
}

Tags:

Misc Example