Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Re-export packages with shorter names (g, h, c) #151

Closed
eduardolat opened this issue Oct 12, 2023 · 3 comments
Closed

Proposal: Re-export packages with shorter names (g, h, c) #151

eduardolat opened this issue Oct 12, 2023 · 3 comments

Comments

@eduardolat
Copy link

The problem:

Right now in the docs it is recommended to import library packages with aliases:

package main

import (
	g "github.com/maragudk/gomponents"
	c "github.com/maragudk/gomponents/components"
	. "github.com/maragudk/gomponents/html"
)

func MyPage() g.Node {
	return h.Main(
		// ...
	)
}

The problem comes when the file is saved, if an import is not being used then go fmt deletes it.

This is a problem because to import packages with aliases there is usually no help from the editor, so you must memorize the import, copy and paste it or create snippets to import the packages.

As you can well imagine, this kills productivity and completely takes you out of the flow state.

Proposed solution:

My proposal to solve this problem is to simply re-export the functions of the 3 main packages so that our import looks like this:

package main

import (
	"github.com/maragudk/gomponents/shortexport/g" // For main package
	"github.com/maragudk/gomponents/shortexport/c" // For components package
	"github.com/maragudk/gomponents/shortexport/h" // For html package
)

func MyPage() g.Node {
	return h.Main( // auto imported when file is saved
		// ...
	)
}

This would attack the problem directly by allowing our code editor tools to auto-import the package without having to write aliases or do it manually, which would be of great help to improve our productivity.

@markuswustenberg
Copy link
Member

Hi @eduardolat, thank you for taking the time to create an issue!

I think this is usually something you can set up in your editor. Either you could have it not delete unused imports, or use an editor that supports importing with aliases. GoLand (from Jetbrains) usually does this quite well, I don't know about other editors. Alternatively, wait with saving the file until you've used the import.

This isn't something I want to add to the library. If you editor doesn't support some of my proposed solutions, maybe you can file an issue with the makers of your editor?

I hope you find a good solution. 😊

@eduardolat
Copy link
Author

Thaks @markuswustenberg for your response.

I think I'll just use the imports without aliases (gomponents, html, components instead of g, h, c).

I'm going to find out about configurations for vscode that solve this in the best possible way, thank you very much for your contributions to the community.

@markuswustenberg
Copy link
Member

I'm glad you found a workaround that works for you. Feel free to post a solution here if you ever find one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants