2
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 ?

  • `v run ./` instead of `v run main.v` would include function from `beta.v`. – Adam Oates Apr 14 '22 at 04:07
  • I just tried recreating the folder structure you provided and it's giving the same error. I don't think it's supposed to do that, so it's probably a bug with V itself. – Adam Oates Apr 14 '22 at 04:08
  • @AdamOates, they mentioned a similar submodule program in vlang/examples/submodule. It is working fine, try once if possible!! just open your git pull and run, it somehow works!~~ – Immature trader Apr 17 '22 at 10:39
  • 1
    do you have a `v.mod` file? – kendfss Feb 28 '23 at 04:33

1 Answers1

1

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.

Ferdinand Prantl
  • 5,281
  • 34
  • 35