Why can't I add a main to my library in golang?

Ethan :

I'm having trouble achieving what should be an easy task. I understand the GitHub model for code organization (ie a library repo and an app repo that consumes the library). I think it's fantastic. But I find often that I want mylib to come bundled with a simple executable in a single main.go file. The main.go should be package main and should import mylib. In other words, it should be an exact documentation of how to build an app that consumes this library.

My point is, since it is often enough convenient to provide a simple command line interface that wraps your library, there should be an easy way to do this without having to make another repo, and golang should help.

I'd like something like the following:

$GOPATH/src/github.com/me/mylib
    mylib.go
    mylib_also.go
    main.go

where mylib is the library (package mylib) and main.go is package main and on running go install it generates bin/mylib and pkg/mylib.a.

Either main.go should import "github.com/me/mylib" (if I do that now, I get cyclical import error) or go would understand what's happening since this feature should be built in and the one main.go in the repo generates the exec. Probably requiring the import (and dropping the cyclical error) is the better way.

Right now, I have to do

$GOPATH/src/github.com/me/mylib
    mylib/
        mylib.go
    main.go

So I have to import github.com/me/mylib/mylib which is ridiculous.

In sum, go install should allow the special case of a package and a main which imports the package and provides a simple cli that demonstrates the packages API. The GitHub model promotes two repos, but it should be easy to do simple clis in one!

elithrar :

You can't have multiple packages per folder. Go operates on a package-level, not a file level.

Convention in this case—a binary that consumes your library—is to create a cmd folder with your package main - i.e. as per https://github.com/bradfitz/camlistore/tree/master/cmd or https://github.com/boltdb/bolt

In your case this would be:

$GOPATH/src/github.com/me/mylib
    mylib/
      mylib.go
      README.md
      cmd/
        your-lib-tool/
          main.go

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't Visual Studio find my main function once I add an hlsl/fx file?

Why can't I find the ID using the mgo library of golang?

Why can't I create a method in my main method?

Why can't I require a Java library in my Clojure file?

Why can't I add items into my vector?

Why can't I add a border on my svg path?

Why can't I add localization to my iOS app?

Why can't I add a border on my SVG paths?

Why can't I add more meshes to my threejs scene?

Why can't I get the extra that I put on my main activity

How can I add an external library dependency into my Library?

Why can't I see the queue written to in a subprocess in my main thread in Python?

Why can't I run the statements in main() after my threads stop working?

Why can't I add an image using kivy in the main.py?

I can't add facebook sdk to my android project while using "is check" library

Why can't I use chrono library?

Why can't I use "SupportedOSPlatform" attribute in my .net Standard 2.0 library?

Why can't I import header files to a library in one of my header files?

Word VSTO Add-In C#: Why can't I move my add-in's tab?

Why I can not add my project dependencies?

Why I can't add reference to normal class library in MVC6

Trying to add actions to my main menu with Scene Builder and Netbeans and I don't understand why I'm getting a InvocationTargetException

Why I can't I add dictionaries?

Why can't I open a file by ID that I have installed my add-on in?

NodeJS - why can't I use "Await" in my main program? Hangs... what is a "module" and what is "top level" - i.e

why can't we add elements to ArrayList outside of the main() method?

Why i can't return my method?

Why can't I call my function?

Why can't I delete my files?