[Golang] Create a package

A new package must be saved in a file under $Gopath/src

Step: 


  1. A new package file name "isEven.go" and save it in a folder with a same name
  2. The folder isEven is save under $Gopath/src.
  3. The folder isEven Path is  $Gopath/src/isEven
  4. 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.")
}

留言

這個網誌中的熱門文章

[Python] raw_input() function

[Golang] for