[Golang] Create a package
A new package must be saved in a file under $Gopath/src
Step:
Step:
- A new package file name "isEven.go" and save it in a folder with a same name
- The folder isEven is save under $Gopath/src.
- The folder isEven Path is $Gopath/src/isEven
- Import the package: import "isEven"
Ex.: isEven.go
//package name
package isEven
func IsEven(input int) bool {
devidedByTwo := true
if input%2 != 0 {
devidedByTwo = false
}
return devidedByTwo
}
Ex. test.go
package main
import (
"fmt"
//import the package
"isEven"
)
var returnValue bool
func main() {
i := 20
if returnValue = isEven.IsEven(i); returnValue != true{
fmt.Printf("Entered number is an odd.")
return
}
fmt.Printf("Entered number is an even.")
}
留言
張貼留言