Vlang module calls confusion

422 Views Asked by At
Basics
| main.v
| beta.v
|
|__ parent
   | mod1.v
   |
   |__ child
      | mod2.v

Codes:

main.v

import parent
import parent.child as pc

fn main(){
  parent.name_parent()
  pc.name_child()
}

mod1.v

module parent

pub fn name_parent(){
  println('Parent!!!')
}

mod2.v

module child

pub fn name_child(){
  println('child!!!')
}

beta.v

pub fn beta_test(){
  println('Beta!!!')
}

Need some insight on the module structure:

  1. Error when I run main.v to access child directory.

    *error: unknown function: parent.child.name_child*
    
  2. How to access beta.v function from main.v ?

1

There are 1 best solutions below

0
On

As @Immature trader suggested, the solution is to create a file v.mod in the root of your module. For example:

Basics
| v.mod    << added
| main.v
| beta.v
...

The initial contents of v.mod, for example:

Module {
  name: 'Basics',
  description: 'Test submodules.',
  version: '0.0.1'
  dependencies: []
}

Compiling and running works:

❯ v .
❯ v run .
Parent!!!
child!!!

You need to set the root directory for the resolution of relative file paths within a V module. That's what the file v.mod does. Unfortunately, it's not documented in the chapter about V modules. There's an open issue to add this information to the documentation.