#golang #go #packages
Custom Packages and Functions
-----------------------------
A. Blank identifier - _ "underscore"
B. Godoc - Utility to get package documentation about all the go packages
C. Returning values from functions - User defined functions
D. Custom packages
Note:-
Function names starts with lowercase alphabets will not be exported to use outside from a go program
Function names started with uppercase letters are only can be used outside the go program or package
1. Set up GOPATH to the current working directory/ root of the package
2. Inside GOPATH directory create src/packagename ex:- GOPATH/src/hello - Hello is the name of the package
3. Create Package Hello (not a command)
4. Switch over to the GOPATH directory
5. Create a go program to import and use the custom package hello
Source Code
---------------
blankdemo.go
--------------
package main
import ("fmt"
_ "strings"
)
func main(){
fmt.Println("Blank demo")
}
funcdemo.go
-------------
package main
import "fmt"
func main(){
show() //Call Function Show
display("Calling display with argument")
result:=add(10,20)
fmt.Printf("The sum is %d\n",result)
}
func show(){
fmt.Println("Inside Show Function")
}
func display(message string){
fmt.Println(message)
}
func add(first int,second int)(int){ //Return values from the function
sum:=first+second
return sum
}
mathematics.go (create it inside GOPATH/src/mathematics/)
-------------
package mathematics
func Add(a int,b int)(int){
return a+b
}
func Sub(a int,b int)(int){
return a-b
}
func Mul(a int,b int)(int){
return a*b
}
func Div(a int,b int)(int){
return a/b
}
calc.go (create it inside GOPATH)
-------
package main
import ("mathematics"
"fmt"
)
func main(){
a:=20
b:=10
fmt.Printf("The sum of %d and %d is %d \n",a,b,mathematics.Add(a,b))
fmt.Printf("The difference of %d and %d is %d\n",a,b,mathematics.Sub(a,b))
fmt.Printf("The product of %d and %d is %d\n",a,b,mathematics.Mul(a,b))
fmt.Printf("The division of %d and %d is %d\n",a,b,mathematics.Div(a,b))
}
Watch video Go (golang) Tutorials - Custom Packages and Functions online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 12 March 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 1,759 times and liked it 26 visitors.