ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - gone-io/gone: Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services.
Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services. - gone-io/gone
Visit Site

GitHub - gone-io/gone: Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services.

GitHub - gone-io/gone: Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services.

license GoDoc Go Report Card codecov Build and Test Release Mentioned in Awesome Go

Gone

What Is Gone?

Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services.

Features

  • Define the Goner interface to abstract dependencies.
  • Dependency Injection:
    • Inject Goners.
    • Inject function arguments.
  • Modular, detachable design.
  • Startup process control.
  • Testing support.
  • Built-in components:
    • goner/config, supports dependency injection of configuration parameters.
    • goner/tracer, adds TraceId to call chains, supports link tracing.
    • goner/logrus, goner/zap, supports log recording.
    • goner/gin, integrates with the gin framework to provide HTTP request parameter dependency injection.
    • goner/viper, used to parse various configuration files.
    • ...

Dependency Injection and Startup

Here's an example:

package main

import (
	"fmt"
	"github.com/gone-io/gone"
)

type Worker struct {
	gone.Flag // Anonymously embedding gone.Flag structure makes it a Goner, it can be injected as a dependency into other Goners or receive injections from other Goners.
	Name      string
}

func (w *Worker) Work() {
	fmt.Printf("I am %s, and I am working\n", w.Name)
}

type Manager struct {
	gone.Flag                         // Anonymously embedding gone.Flag structure makes it a Goner, it can be injected as a dependency into other Goners or receive injections from other Goners.
	*Worker   `gone:"manager-worker"` // Named injection GonerId="manager-worker" for Worker instance.
	workers   []*Worker               `gone:"*"` // Inject all Workers into an array.
}

func (m *Manager) Manage() {
	fmt.Printf("I am %s, and I am managing\n", m.Name)
	for _, worker := range m.workers {
		worker.Work()
	}
}

func main() {
	managerRole := &Manager{}

	managerWorker := &Worker{Name: "Scott"}
	ordinaryWorker1 := &Worker{Name: "Alice"}
	ordinaryWorker2 := &Worker{Name: "Bob"}

	gone.
		Prepare(func(cemetery gone.Cemetery) error {
			cemetery.
				Bury(managerRole).
				Bury(managerWorker, gone.GonerId("manager-worker")).
				Bury(ordinaryWorker1).
				Bury(ordinary, ordinaryWorker2)
			return nil
		}).
		// Run method supports dependency injection of parameters in its function.
		Run(func(manager *Manager) {
			manager.Manage()
		})
}

Summary:

  1. In the Gone framework, dependencies are abstracted as Goners, which can be injected into each other.
  2. By anonymously embedding the gone.Flag, the structure implements the Goner interface.
  3. Before starting, load all Goners into the framework using the Bury function.
  4. Use the Run method to start, where the function supports dependency injection of parameters.

Full Documentation

Let's use Gone to write a web service below!

🌐Web Service

package main

import (
	"fmt"
	"github.com/gone-io/gone"
	"github.com/gone-io/gone/goner"
)

// Implement a Goner. What is a Goner? => https://goner.fun/guide/core-concept.html#goner-%E9%80%9D%E8%80%85
type controller struct {
	gone.Flag // Goner tag, when anonymously embedded, a structure implements Goner
	gone.RouteGroup `gone:"gone-gin-router"` // Inject root routes
}

// Implement the Mount method to mount routes; the framework will automatically execute this method
func (ctr *controller) Mount() gone.GinMountError {

	// Define request structure
	type Req struct {
		Msg string `json:"msg"`
	}

	// Register the handler for `POST /hello`
	ctr.POST("/hello", func(in struct {
		to  string `gone:"http,query"` // Inject http request Query parameter To
		req *Req   `gone:"http,body"`  // Inject http request Body
	}) any {
		return fmt.Sprintf("to %s msg is: %s", in.to, in.req.Msg)
	})

	return nil
}

func main() {
	// Start the service
	gone.Serve(func(cemetery gone.Cemetery) error {
		// Call the framework's built-in component, load the gin framework
		_ = goner.GinPriest(cemetery)

		// Bury a controller-type Goner in the cemetery
		// What does bury mean? => https://goner.fun/guide/core-concept.html#burying
		// What is a cemetery? => https://goner.fun/guide/core-concept.html#cemetery
		cemetery.Bury(&controller{})
		return nil
	})
}

Run the above code: go run main.go, the program will listen on port 8080. Test it using curl:

curl -X POST 'http://localhost:8080/hello' \
    -H 'Content-Type: application/json' \
	--data-raw '{"msg": "你好呀?"}'

The result is as follows:

{"code":0,"data":"to  msg is: 你好呀?"}

Quick Start

💡Concepts

The code we write is ultimately lifeless unless it is run. In Gone, components are abstracted as Goners, whose properties can inject other Goners. Before Gone starts, all Goners need to be buried in the cemetery; after Gone starts, all Goners will be resurrected to establish a Heaven, "everyone in Heaven is no longer incomplete, and what they want will be satisfied."

Core Concepts

🌰 More Examples:

Detailed examples can be found in the example directory, and more will be completed in the future.

🪜🧰🛠️ Component Library (👉🏻 More components are under development... 💪🏻 ヾ(◍°∇°◍)ノ゙ 🖖🏻)

  • goner/cumx, a wrapper for github.com/soheilhy/cmux, used to reuse the same port to implement multiple protocols;
  • goner/config, used to implement configuration for Gone-App
  • goner/gin, a wrapper for github.com/gin-gonic/gin, providing web services
  • goner/logrus, a wrapper for github.com/sirupsen/logrus, providing logging services
  • goner/tracer, providing log tracing, providing a unified tracer```markdown Id for the same request chain
  • goner/xorm, a wrapper for xorm.io/xorm, used for database access; when using it, import the database driver as needed;
  • goner/redis, a wrapper for github.com/gomodule/redigo, used for interacting with redis
  • goner/schedule, a wrapper for github.com/robfig/cron/v3, used for setting timers
  • emitter, encapsulates event handling, which can be used for DDD's Event Storm
  • goner/urllib, encapsulates github.com/imroc/req/v3, used for sending HTTP requests, and connects the traceId between server and client

📚Full Documentation

Contributing

If you have a bug report or feature request, you can open an issue, and pull requests are also welcome.

Contact

If you have questions, feel free to reach out to us in the following ways:

License

gone released under MIT license, refer LICENSE file.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory