Go Inspect
Small object inspection library inspired from Ruby’s Object#inspect
method for
learning purposes to my self :) Found Go’s reflect
library to play with!
Install
$ go get -u github.com/vigo/go-inspect
Run the test:
$ go test -v .
Usage
It works (as far as I tested) with numerics, strings, arrays, maps and structs.
package main
import (
"fmt"
inspect "github.com/vigo/go-inspect"
)
// User represents basic user data
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
}
// GetFullName returns user's fullname
func (u User) GetFullName() string {
return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}
// SetEmail sets user's email with given input
func (u *User) SetEmail(email string) {
u.Email = email
}
func main() {
user1 := User{
FirstName: "Uğur",
LastName: "Özyılmazel",
Email: "[email protected]",
}
user2 := &User{
FirstName: "Ezel",
LastName: "Özyılmazel",
Email: "[email protected]",
}
fmt.Println(inspect.Element(&user1))
fmt.Println(inspect.Element(user2))
}
Output:
Inspection of: [User] object
[*main.User..............] {Uğur Özyılmazel [email protected]}
# of Fields: 3
FirstName........... string.... Uğur.......................... json:"first_name".............
LastName............ string.... Özyılmazel.................... json:"last_name"..............
Email............... string.... [email protected].............. json:"email"..................
# of Methods: 2
GetFullName......... func(*main.User) string
SetEmail............ func(*main.User, string)
--------------------------------------------------------------------------------------------------------------------
Inspection of: [User] object
[*main.User..............] {Ezel Özyılmazel [email protected]}
# of Fields: 3
FirstName........... string.... Ezel.......................... json:"first_name".............
LastName............ string.... Özyılmazel.................... json:"last_name"..............
Email............... string.... [email protected].............. json:"email"..................
# of Methods: 2
GetFullName......... func(*main.User) string
SetEmail............ func(*main.User, string)
--------------------------------------------------------------------------------------------------------------------
Feel free to fix/improve anything you like to!
Contributer(s)
- Uğur "vigo" Özyılmazel - Creator, maintainer
Contribute
All PR’s are welcome!
fork
(https://github.com/vigo/go-inspect/fork)- Create your
branch
(git checkout -b my-features
) commit
yours (git commit -am 'added killer options'
)push
yourbranch
(git push origin my-features
)- Than create a new Pull Request!
License
This project is licensed under MIT