Search code examples
govgo

How do you create a local VGO application module


Every example I've seen with vgo so far requires you to write your module, upload it then create a new application module to test a basic module.

Is there a way to test a module by skipping the repo step and just adding a package main to your module just to spit out something to the console?

I've tried creating my dummy module module "example.com/m" adding a dummy subpackage to the package example in /example/testing.go then added a simple main.go and tried importing the example package from my dummy module to no avail.

If I had no internet, I'd be pretty lost with the vgo workflow (I'm lost without it tbh)


Solution

  • I figured it out. Basically vgo allows you to include package main in the root, but by doing this you can only use 'main' packages in the root directory of your vgo project because golang only permits one package name per folder. Which is fine since you can remove the main file later if you want.

    Answer

    • Create a package main file in the root directory of your vgo project with a fn main

    • Create any amount of sub packages in your vgo project ex.: package example in an example sub folder. For fun create an exported function that prints out "Hello World" and call it "WorldGreeting"

    • Now in your main file you can import this package by calling your module name followed by the sub package name. So for instance if your module name is bitbucket.com/User/MyVGOPackage then you would import your sub package in the main file like import "bitbucket.com/User/MyVGOPackage/example" then call your function inside of main example.WorldGreeting()
    • Build using go build to build the full directory (in case you have more main packages in the directory) and vgo will spit out an exe called "MyVGOPackage.exe"

    Tip: If you just want to create an application without a weird module name just rename your module to something simple like "MyApplication" and vgo will understand your imports perfectly. then go back and replace "bitbucket.com/User/MyVGOPackage/example" to "MyApplication/example" and everything will build normally. Jetbrain's GoLand IDE understands this usage perfectly so give it a shot if vs code is having issues auto completing.