其他分享
首页 > 其他分享> > [Go] Package

[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

标签:package,int,fmt,Package,num,file,Go,total
来源: https://www.cnblogs.com/Answer1215/p/16658146.html