[Go] Package

Create a new folder utilswith a new file math.go

package utils

import (
  "fmt"
)

func printNum(num int) {
    fmt.Println("Current number is: ", num)
}

// Add multi int number together return total int
func Add(nums ...int) int {
    total := 0
    for _, num := range nums {
        printNum(num)
        total += num
    }
    return total
}

 

In Go package, any funcwith captlized name will be auto exported. And you must add comments for that function.

Any funcwith lower case name will be private to that file.

 

Import the local package:

package main

import (
  "fmt"
   math "<path_to_folder>/utils"
)

So, we import the file, and named it as math. You can also use default package name utils

posted @ 2022-09-05 14:49  Zhentiw  阅读(20)  评论(0编辑  收藏  举报