# Module with global variables vs initialization with \`\_\_init\_\_\` function

**URL:** <https://discourse.julialang.org/t/module-with-global-variables-vs-initialization-with-init-function/123668>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 10, 2024, 3:42pm UTC](https://discourse.julialang.org/t/module-with-global-variables-vs-initialization-with-init-function/123668 "2024-12-10T15:42:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 10, 2024, 3:42pm UTC](https://discourse.julialang.org/t/module-with-global-variables-vs-initialization-with-init-function/123668/1 "2024-12-10T15:42:44Z")

</div>

Quick query about modules.

Is there any difference between writing a module with initialized global variables and using an ` __init__ ` function to initialize those variables?

Allow me to explain with two demonstrations modules:

```julia
# ModuleA.jl

module ModuleA

global gCache = Dict()

end

```

```julia
# ModuleB.jl

module ModuleB

function __init__ ()
    global gCache
    gCache = Dict()
end

end

```

I’m curious as to whether there is any material difference between these two codes.

In addition, there is an auxillary point related to VS Code and the Julia Extension.

- In the case of `ModuleA`, the Julia Extension recognizes the variable `gCache`
- In the case of `ModuleB`, the Julia Extension discorages writing code in this way as it marks the global variable `gCache` as an undefined reference

I wonder if anyone can comment on this. Is this simply a bug in the Julia Extension? Or is something more significant going on here?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 10, 2024, 4:17pm UTC](https://discourse.julialang.org/t/module-with-global-variables-vs-initialization-with-init-function/123668/2 "2024-12-10T16:17:41Z")

</div>

You should probably just do version A. Thr `global` keyword there is redundant. This will run as a top level statement and will be cached at precompile time.

` __init__ ` can be useful when you need to execute code at _runtime_ initialization. This is for things that are different on each run or depend on external environment variables. In light of static compilation via juliac, use of **init** is highly discouraged.

If you want to see the difference try to use `rand()` and make the module a package.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 10, 2024, 4:21pm UTC](https://discourse.julialang.org/t/module-with-global-variables-vs-initialization-with-init-function/123668/3 "2024-12-10T16:21:46Z")

</div>

(edit: woops - replied to the wrong post)

You’ve just answered my next question, which I just posted [here](https://discourse.julialang.org/t/use-of-global-in-a-module-for-global-variable-is-pointless/123669)
