- 主题:go的初始化顺序,竟然没有固定的顺序?
网上说 首先初始化 导入包里的init,可是我执行代码,有时候首先初始化 main 里的 init 函数。
初始化,没有固定顺序?
--
FROM 120.242.252.*
好像是按照文件名排序吧
【 在 feng321 的大作中提到: 】
: [upload=1][/upload]
: 网上说 首先初始化 导入包里的init,可是我执行代码,有时候首先初始化 main 里的 init 函数。
: 初始化,没有固定顺序?
--
FROM 114.242.248.*
https://zhuanlan.zhihu.com/p/34211611我觉得这些语法点比较生僻, 能不用就不用最好.
【 在 feng321 的大作中提到: 】
: [upload=1][/upload]
: 网上说 首先初始化 导入包里的init,可是我执行代码,有时候首先初始化 main 里的 init 函数。
: 初始化,没有固定顺序?
--
FROM 117.176.243.*
能放完整程序么,看起来 import 包并没有被调用
【 在 feng321 的大作中提到: 】
: 网上说 首先初始化 导入包里的init,可是我执行代码,有时候首先初始化 main 里的 init 函数。
: 初始化,没有固定顺序?
- 来自「最水木 for iPhone13,2」
--
FROM 115.192.191.*
这个是 geometry.go
// geometry.go
package main
import (
"fmt"
"log"
"third_go_modules/src/geometry/rectangle" // 导入自定义包
)
/*
* 1. 包级别变量
*/
var rectLen, rectWidth float64 = 6, 7
/*
*2. init 函数会检查长和宽是否大于0
*/
func init() {
println("main package initialized")
if rectLen < 0 {
log.Fatal("length is less than zero")
//fmt.Println("length is less than zero")
}
if rectWidth < 0 {
log.Fatal("width is less than zero")
//fmt.Println("width is less than zero")
}
}
func main() {
//fmt.Println("Geometrical shape properties")
//var rectLen, rectWidth float64 = 6, 7
fmt.Println("Geometrical shape properties")
/*Area function of rectangle package used*/
fmt.Printf("area of rectangle %.2f\n", rectangle.Area(rectLen, rectWidth))
/*Diagonal function of rectangle package used*/
fmt.Printf("diagonal of the rectangle %.2f ", rectangle.Diagonal(rectLen, rectWidth))
}
【 在 StephenLee 的大作中提到: 】
: 能放完整程序么,看起来 import 包并没有被调用
: - 来自「最水木 for iPhone13,2」
--
FROM 120.242.252.*
这个是rectprops.go
// rectprops.go
package rectangle
import (
"fmt"
"math"
)
/*
* init function added
*/
func init() {
fmt.Println("rectangle package initialized")
}
func Area(len, wid float64) float64 {
area := len * wid
return area
}
func Diagonal(len, wid float64) float64 {
diagonal := math.Sqrt((len * len) + (wid * wid))
return diagonal
}
【 在 StephenLee 的大作中提到: 】
: 能放完整程序么,看起来 import 包并没有被调用
: - 来自「最水木 for iPhone13,2」
--
FROM 120.242.252.*
我拿这代码测了没问题啊。是不是模块路径有问题,按你的项目结构,主包的名字应该是 geometry,不该是 third_xxxx,贴一下 go.mod 。
rectangle package initialized
main package initialized
Geometrical shape properties
area of rectangle 42.00
diagonal of the rectangle 9.22 %
【 在 feng321 的大作中提到: 】
: 这个是rectprops.go
: // rectprops.go
: package rectangle
: ...................
--
FROM 115.193.174.*
关键是输出顺序不固定,我偶尔也有你这样的输出
main package initialized
rectangle package initialized
Geometrical shape properties
area of rectangle 42.00
diagonal of the rectangle 9.22
【 在 StephenLee 的大作中提到: 】
: 我拿这代码测了没问题啊。是不是模块路径有问题,按你的项目结构,主包的名字应该是 geometry,不该是 third_xxxx,贴一下 go.mod 。
: rectangle package initialized
: main package initialized
: ...................
--
FROM 120.242.252.*
go.mod
module third_go_modules
go 1.15
【 在 StephenLee 的大作中提到: 】
: 我拿这代码测了没问题啊。是不是模块路径有问题,按你的项目结构,主包的名字应该是 geometry,不该是 third_xxxx,贴一下 go.mod 。
: rectangle package initialized
: main package initialized
: ...................
--
FROM 120.242.252.*
third_go_modules如果是主包的话,go.mod应该在主包的目录,跟src平级。谨慎怀疑这里混用了go module和go path。
【 在 feng321 的大作中提到: 】
: go.mod
:
: module third_go_modules
: ....................
- 来自「最水木 for iPhone13,2」
--
FROM 115.193.174.*