package tools
import (
"fmt"
"golang/calc"
)
func init() {
fmt.Println("tools init..")
}
func PrintInfo() {
calc.Add(10, 20)
fmt.Println("tools print..")
}
package calc
import "fmt"
func init() {
fmt.Println("calc init..")
}
func Add(x, y int) int {
return x + y
}
package main
import (
"fmt"
"golang/tools"
)
func init() {
fmt.Println("main init..")
}
func main() {
tools.PrintInfo()
}
package main
import (
"fmt"
"itying/calc"
"itying/tools"
)
func main() {
sum := calc.Add(10, 2)
fmt.Println(sum) //12
// fmt.Println(calc.aaa)
sub := calc.Sub(10, 2)
fmt.Println(sub)
//调用tools包里面的方法
b := tools.Mul(2, 6)
fmt.Println(b)
}
package tools
func Mul(x, y int) int {
return x * y
}
package tools //package 一定要放在最上面
import "fmt"
func SortIntAsc() {
fmt.Println("按照升序对int切片排序")
}
package tools
import "fmt"
func PrintInfo() {
fmt.Println("打印信息")
}
package calc
var aaa = "私有变量" //首字母小写表示私有
var Age = 20
func Add(x, y int) int { //首字母大写表示 公有方法
return x + y
}
func Sub(x, y int) int { //公有方法
return x - y
}
package main
import (
"fmt"
"github.com/shopspring/decimal"
"github.com/tidwall/gjson"
)
func main() {
// var num1 float64 = 3.1
// var num2 float64 = 4.2
// fmt.Println(num1 + num2) //7.300000000000001
// //加
var num1 float64 = 3.1
var num2 float64 = 4.2
d1 := decimal.NewFromFloat(num1).Add(decimal.NewFromFloat(num2))
fmt.Println(d1)
//减去
m1 := 8.2
m2 := 3.8
m3 := decimal.NewFromFloat(m1).Sub(decimal.NewFromFloat(m2))
fmt.Println(m3)
// 减法 Sub,乘法 Mul, 除法 Div 用法均与上述类似
f1 := 1.2
f2 := 3.8
f3 := decimal.NewFromFloat(f1).Mul(decimal.NewFromFloat(f2))
fmt.Println(f3)
const json = `{"name":{"first":"Janet","last":"四"},"age":47}`
value := gjson.Get(json, "name.last")
println(value.String())
}