Tuesday, September 8, 2020

Golang: Packages


import alias:
package main

import f "fmt"

func main(){

 f.Println("adsasds")

}

greetings.go
package greetingspackage
import "fmt"
func PrintGreetings(){
 fmt.Println("I'm printing a message from printgreetings")
}
func printGreetingsUnexported(){
 fmt.Println("I'm printing a message from printgreetings unexported")
}

Note: captial first letter functioname can be exported.
GopherGreetings.go
package greetingspackage
import "fmt"
var MagicNumber int
func GopherGreetings(){
fmt.Println("Gopher greetings function")
printGreetingsUnexported()//As same package
}

//executed first
func init(){
MagicNumber = 108
}

useGreetings.go
package main
import(
 "fmt"
 "github.com/EngineerKamesh/gofullstack/volume1/section2/greetingspackage"
)
func main(){
 greetingspackage.PrintGreetings()
 greetingspackage.GopherGreetings()
 fmt.Println("The value of Magic Number is:",greetingpackage.MagicNumber)
}

go install
usegreetings

No comments:

Post a Comment