English | ä¸ć–‡
- gonectl
- Introduction
- Installation
- Feature Overview
- Detailed Usage Guide
- 1. create Command: Create Gone Projects from Templates
- 2. install Command: Install Gone Modules and Generate
module.load.go
- gone-io Official Modules, Supporting Short Names
- 3. generate Command: Generate
*.gone.go
Files for Gone Projects - 4. mock Command: Generate Mock Code
- 5. build Command: Build Gone Projects
- 6. run Command: Run Gone Projects
- FAQ
- More Resources
Command-line tool for Gone framework, simplifying project creation, module management, and code generation
gonectl
is the official command-line tool for the Gone framework, designed to streamline the development process of Gone projects. It provides a series of convenient commands to help developers quickly create projects, manage modules, generate code, and build applications. Whether you're new to Gone or an experienced developer, gonectl
can significantly improve your development efficiency.
Run the following command to install gonectl
:
go install github.com/gone-io/gonectl@latest
After installation, gonectl
will be located in the $GOPATH/bin
directory. Make sure this directory is added to your system's $PATH
environment variable for global access to the gonectl
command.
Tip: If you're unsure about the location of
$GOPATH
, you can check it by runninggo env GOPATH
.
You can also visit the gonectl/releases page to download the latest version binary for your operating system, then:
- Extract the downloaded file
- Copy the extracted
gonectl
executable to a directory in your system PATH - Ensure the file has execution permissions (on Linux/macOS, you may need to run
chmod +x gonectl
)
gonectl
provides the following core features:
- Project Creation: Quickly scaffold Gone project architecture from templates
- Module Installation: Integrate Gone modules and automatically generate loading code
- Code Generation: Automatically generate necessary Gone framework integration code
- Mock Generation: Create Mock implementations for interfaces, facilitating unit testing
- Build and Run: Simplify project building and running processes
The create
command helps you quickly create Gone projects based on preset or custom templates.
gonectl create -h
gonectl create demo-project
This will create a basic Gone project named demo-project
in the current directory.
gonectl create demo-project -t template-name
gonectl create -ls
This command lists all built-in project templates with their brief descriptions.
gonectl create demo-project -t template-name -m github.com/gone-io/my-module
This is particularly useful when creating projects that will be published as public packages.
gonectl create demo-project -t https://github.com/gone-io/template-v2-web-mysql
You can directly use any Git repository that follows the Gone template specification as a project template.
The install
command integrates Gone modules into your project and automatically generates the necessary loading code.
Gone Module Best Practice: We recommend each Gone module to provide one or more
gone.LoadFunc
functions, such as:func Load(gone.Loader) error { // Load related Goners return nil }
gonectl install -h
gonectl install demo-module
This adds demo-module
to your project and generates the corresponding loading code.
# Specify LoadA and LoadB functions for generating loading code
gonectl install module LoadA,LoadB
gonectl install github.com/gone-io/goner/nacos RegistryLoad
This installs the nacos module and uses its RegistryLoad
function for initialization.
When executing gonectl install module
command:
- If the module is not installed, it will be installed
- If already installed, an interactive selection list will be displayed where you can uncheck unwanted LoadFunc to remove them from
module.load.go
gonectl install goner/nacos
Note: For unofficial modules, you need to use the complete Golang module name.
The generate
command scans project directories and automatically generates integration code files needed by the Gone framework.
This command will:
-
Scan all packages in specified directories
-
Create
init.gone.go
file for packages containing Goner or LoadFunc, generating automatic loading code:func init() { gone. Loads(Load). // Load LoadFunc Load(&MyGoner{}) // Load Goner // ... Load more Goners }
Note: If a package defines
LoadFunc
, it will only loadLoadFunc
and not directly load Goners, indicating that the user has chosen to manually manage Goners. -
Create
import.gone.go
file in the main package directory to import all discovered Goner packages:package main import ( _ "test" _ "test/modules/a" _ "test/modules/b" )
Important: Do not manually modify
*.gone.go
files, as they will be automatically overwritten bygonectl
.
# Can specify multiple directories simultaneously
gonectl generate -s ./test -s ./test2
gonectl generate -m cmd/server
gonectl generate -m for_import --main-package-name for_import
When using multiple Gone instances in the same program, you can use --preparer-code
and --preparer-package
parameters:
# Goners in gone1 directory use instance-1 instance
gonectl generate -s gone1 --preparer-code 'g.App("instance-1")' --preparer-package 'github.com/gone-io/goner/g'
# Goners in gone2 directory use instance-2 instance
gonectl generate -s gone2 --preparer-code 'g.App("instance-2")' --preparer-package 'github.com/gone-io/goner/g'
Create a generate.go
file in the project root directory and add the following code:
//go:generate gonectl generate -m main-package-dir
Then execute go generate ./...
to automatically run the gonectl command.
The mock
command generates Mock implementations for interfaces and registers them as Goners, facilitating integration into the Gone framework for testing.
Prerequisites: This feature depends on the
uber mockgen
tool, please install it first:go install go.uber.org/mock/mockgen@latest
gonectl mock -h
# Generate Mock implementation for UserService interface in service package
gonectl mock -package service -interfaces UserService
# Generate Mock implementations for multiple interfaces and specify output directory
gonectl mock -package service -interfaces "UserService,OrderService" -output ./mocks
The build
command is an enhanced wrapper around the standard go build
, specifically designed for Gone projects.
- Automatically executes
go generate ./...
before compilation to ensure all auxiliary code is updated - Supports all standard
go build
parameters and options
gonectl build -h
# Build Gone project in current directory
gonectl build
# Specify output filename
gonectl build -o myapp
# Use other go build parameters
gonectl build -v -ldflags="-s -w"
The run
command is similar to build
, serving as an enhanced wrapper around go run
.
- Automatically runs
go generate ./...
before execution to update all auxiliary code - Supports all standard
go run
parameters and options
gonectl run -h
# Run Gone project in current directory
gonectl run
# Run specific file
gonectl run main.go
# Run with parameters
gonectl run . -config=dev.yaml
A: gonectl is a complement to standard Go tools, specifically designed for the Gone framework. It simplifies Gone-specific code generation and project management processes but still internally calls standard Go commands.
A: Execute go install github.com/gone-io/gonectl@latest
to update to the latest version.
A: It's recommended to include these files in version control as they are part of the project structure. However, they can also be dynamically generated in CI/CD pipelines.