# Gone's Hook Functions
# What are Hook Functions?
In the process of starting and stopping Gone, support is provided to register four types of functions that will be executed at specific times, known as Hook functions:
- BeforeStart Hook function: Executes before Gone starts, registered using
BeforeStart
. - AfterStart Hook function: Executes after Gone starts, registered using
AfterStart
. - BeforeStop Hook function: Executes before Gone stops, registered using
BeforeStop
. - AfterStop Hook function: Executes after Gone stops, registered using
AfterStop
.
# Code Example
package main
import "github.com/gone-io/gone"
type Worker struct {
gone.Flag
Name string
}
type Boss struct {
gone.Flag
Name string
}
func main() {
gone.
Prepare(func(cemetery gone.Cemetery) error {
cemetery.Bury(&Boss{Name: "Jim"}, "boss-jim")
cemetery.Bury(&Worker{Name: "Bob"}, "worker-bob")
return nil
}).
BeforeStart(func() {
println("First BeforeStart function")
}).
BeforeStart(func(in struct {
worker Worker `gone:"worker-bob"`
boss Boss `gone:"*"`
}) {
println("Second BeforeStart function")
println("Boss:", in.boss.Name)
println("Worker:", in.worker.Name)
}).
BeforeStart(func() error {
println("Third BeforeStart function")
return nil
}).
Run()
}
The output of the above code is as follows:
Third BeforeStart function
Second BeforeStart function
Boss: Jim
Worker: Bob
First BeforeStart function
# Rules for Hook Functions
- Multiple registrations of the same type of Hook function are allowed.
- For
BeforeStart
andBeforeStop
Hook functions, the ones registered later are executed first. - For
AfterStart
andAfterStop
Hook functions, the ones registered earlier are executed first. - Hook functions can be registered using the
Preparer
object returned bygone.Prepare(priest)
, supporting chaining. - The types of functions that can be registered with the
Preparer
object include:- Input Parameters:
- No parameters
- Anonymous struct parameters, with properties annotated with
gone
tags automatically injected with corresponding values.
- Return Parameters:
- No return parameters
error
parameter; if the error parameter is notnil
, the program will panic when it reaches the Hook, terminating execution.
- Input Parameters:
# Gone's Startup Process
- Revive all buried Goners.
- Execute the
AfterRevive
method of all prophet Goners. - Register the
Start
method of angels as BeforeStart Hook functions and theStop
method as BeforeStop Hook functions. - Register Hook functions for business code.
- Execute BeforeStart Hook functions in order.
- Execute AfterStart Hook functions in order.
- Wait for the program to end.
- Execute BeforeStop Hook functions in order.
- Execute AfterStop Hook functions in order.