Skip to content

Commit

Permalink
Note some constraints for package initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
upamanyus committed Nov 25, 2024
1 parent 4609cf6 commit cea5e07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,14 @@ func (ctx Ctx) maybeDecls(d ast.Decl) []glang.Decl {
}

func (ctx Ctx) initFunction() glang.Decl {
fd := glang.FuncDecl{Name: "init'"}
// TODO: some constraints for the init translation:
// - Variables are initialized before `init()` runs
// - `init()` can be defined multiple times, and are run in source order.
// - Need to fully initialize lower level packages (e.g. call `init()`s) before
// initializing vars of the current package.
// - should only `init` a package once, even if imported multiple times.

fd := glang.FuncDecl{Name: "initialize'"}

fd.Body = glang.Tt

Expand Down
8 changes: 8 additions & 0 deletions testdata/examples/unittest/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ func bar() {
panic("bad")
}
}

func init() {
GlobalX = GlobalX
}

func init() {
globalY = ""
}

0 comments on commit cea5e07

Please sign in to comment.