Command-line device for Gone framework, simplifying mission creation, module administration, and code era
Introduction
gonectr
is the official command-line device for the Gone framework, designed to streamline the event strategy of Gone tasks. It offers a collection of handy instructions to assist builders shortly create tasks, handle modules, generate code, and construct functions. Whether or not you’re new to Gone or an skilled developer, gonectr
can considerably enhance your growth effectivity.
Set up
Technique 1: Utilizing go set up (Advisable)
Run the next command to put in gonectr
:
go set up github.com/gone-io/gonectr@newest
After set up, gonectr
might be situated within the $GOPATH/bin
listing. Be certain that this listing is added to your system’s $PATH
surroundings variable for international entry to the gonectr
command.
Tip: In the event you’re not sure in regards to the location of
$GOPATH
, you may test it by operatinggo env GOPATH
.
Technique 2: Direct Binary Obtain
You can too go to the gonectr/releases web page to obtain the newest model binary in your working system, then:
-
Extract the downloaded file
-
Copy the extracted
gonectr
executable to a listing in your system PATH -
Make sure the file has execution permissions (on Linux/macOS, chances are you’ll must run
chmod +x gonectr
)
Characteristic Overview
gonectr
offers the next core options:
-
Challenge Creation: Shortly scaffold Gone mission structure from templates
-
Module Set up: Combine Gone modules and mechanically generate loading code
-
Code Technology: Robotically generate obligatory Gone framework integration code
-
Mock Technology: Create Mock implementations for interfaces, facilitating unit testing
-
Construct and Run: Simplify mission constructing and operating processes
Detailed Utilization Information
1. create Command: Create Gone Tasks from Templates
The create
command helps you shortly create Gone tasks based mostly on preset or customized templates.
View Assist:
gonectr create -h
Fundamental Utilization: Create Challenge
gonectr create demo-project
This can create a fundamental Gone mission named demo-project
within the present listing.
Create Challenge with Particular Template
gonectr create demo-project -t template-name
Listing All Out there Templates
gonectr create -ls
This command lists all built-in mission templates with their temporary descriptions.
Create Challenge with Module Title
gonectr create demo-project -t template-name -m github.com/gone-io/my-module
That is notably helpful when creating tasks that might be printed as public packages.
Create Challenge from Distant Git Repository Template
gonectr create demo-project -t https://github.com/gone-io/template-v2-web-mysql
You may instantly use any Git repository that follows the Gone template specification as a mission template.
2. set up Command: Set up Gone Modules and Generate module.load.go
The set up
command integrates Gone modules into your mission and mechanically generates the mandatory loading code.
Gone Module Finest Apply: We suggest every Gone module to supply a number of
gone.LoadFunc
features, equivalent to:
func Load(gone.Loader) error {
// Load associated Goners
return nil
}
View Assist:
gonectr set up -h
Fundamental Utilization: Set up Module
gonectr set up demo-module
This provides demo-module
to your mission and generates the corresponding loading code.
Specify LoadFunc
# Specify LoadA and LoadB features for producing loading code
gonectr set up module LoadA,LoadB
Actual Instance
gonectr set up github.com/gone-io/goner/nacos RegistryLoad
This installs the nacos module and makes use of its RegistryLoad
operate for initialization.
Uninstall/Modify Module
When executing gonectr set up module
command:
-
If the module just isn't put in, will probably be put in
-
If already put in, an interactive choice checklist might be displayed the place you may uncheck undesirable LoadFunc to take away them from
module.load.go
gone-io Official Modules, Supporting Brief Names
gonectr set up goner/nacos
Be aware: For unofficial modules, you'll want to use the entire Golang module identify.
3. generate Command: Generate *.gone.go
Information for Gone Tasks
The generate
command scans mission directories and mechanically generates integration code recordsdata wanted by the Gone framework.
Performance
This command will:
-
Scan all packages in specified directories
-
Create
init.gone.go
file for packages containing Goner or LoadFunc, producing automated loading code:
func init() {
gone.
Hundreds(Load). // Load LoadFunc
Load(&MyGoner{}) // Load Goner
// ... Load extra Goners
}
Be aware: If a bundle defines
LoadFunc
, it'll solely loadLoadFunc
and never instantly load Goners, indicating that the consumer has chosen to manually handle Goners.
- Create
import.gone.go
file in the principle bundle listing to import all found Goner packages:
bundle foremost
import (
_ "take a look at"
_ "take a look at/modules/a"
_ "take a look at/modules/b"
)
Vital: Don't manually modify
*.gone.go
recordsdata, as they are going to be mechanically overwritten bygonectr
.
Specify Scan Listing
# Can specify a number of directories concurrently
gonectr generate -s ./take a look at -s ./test2
Specify Predominant Perform Listing
gonectr generate -m cmd/server
Superior Utilization: Generate import.gone.go
for Non-main Bundle
gonectr generate -m for_import --main-package-name for_import
Superior Utilization: Help A number of Gone Situations
When utilizing a number of Gone situations in the identical program, you should utilize --preparer-code
and --preparer-package
parameters:
# Goners in gone1 listing use instance-1 occasion
gonectr generate -s gone1 --preparer-code 'g.App("instance-1")' --preparer-package 'github.com/gone-io/goner/g'
# Goners in gone2 listing use instance-2 occasion
gonectr generate -s gone2 --preparer-code 'g.App("instance-2")' --preparer-package 'github.com/gone-io/goner/g'
Use with go generate
Create a generate.go
file within the mission root listing and add the next code:
//go:generate gonectr generate -m main-package-dir
Then execute go generate ./...
to mechanically run the gonectr command.
4. mock Command: Generate Mock Code
The mock
command generates Mock implementations for interfaces and registers them as Goners, facilitating integration into the Gone framework for testing.
Stipulations: This characteristic is dependent upon the
uber mockgen
device, please set up it first:
go set up go.uber.org/mock/mockgen@newest
View Assist:
gonectr mock -h
Fundamental Utilization
# Generate Mock implementation for UserService interface in service bundle
gonectr mock -package service -interfaces UserService
Extra Choices
# Generate Mock implementations for a number of interfaces and specify output listing
gonectr mock -package service -interfaces "UserService,OrderService" -output ./mocks
5. construct Command: Construct Gone Tasks
The construct
command is an enhanced wrapper round the usual go construct
, particularly designed for Gone tasks.
Options
View Assist:
gonectr construct -h
Fundamental Utilization
# Construct Gone mission in present listing
gonectr construct
# Specify output filename
gonectr construct -o myapp
# Use different go construct parameters
gonectr construct -v -ldflags="-s -w"
6. run Command: Run Gone Tasks
The run
command is much like construct
, serving as an enhanced wrapper round go run
.
Options
View Assist:
gonectr run -h
Fundamental Utilization
# Run Gone mission in present listing
gonectr run
# Run particular file
gonectr run foremost.go
# Run with parameters
gonectr run . -config=dev.yaml
FAQ
Q: What's the relationship between gonectr and normal Go instruments?
A: gonectr is a complement to straightforward Go instruments, particularly designed for the Gone framework. It simplifies Gone-specific code era and mission administration processes however nonetheless internally calls normal Go instructions.
Q: improve gonectr to the newest model?
A: Execute go set up github.com/gone-io/gonectr@newest
to replace to the newest model.
Q: Ought to *.gone.go recordsdata be included in model management?
A: It’s really useful to incorporate these recordsdata in model management as they're a part of the mission construction. Nonetheless, they will also be dynamically generated in CI/CD pipelines.