In Java I can do something like
derp(new Runnable { public void run () { /* run this sometime later */ } })
and "run" the code in the method later. It's a pain to handle (anonymous inner class), but it can be done.
Does Go have something that can facilitate a function/callback being passed in as a parameter?
Yes, consider some of these examples:
package main
import "fmt"
// convert types take an int and return a string value.
type convert func(int) string
// value implements convert, returning x as string.
func value(x int) string {
return fmt.Sprintf("%v", x)
}
// quote123 passes 123 to convert func and returns quoted string.
func quote123(fn convert) string {
return fmt.Sprintf("%q", fn(123))
}
func main() {
var result string
result = value(123)
fmt.Println(result)
// Output: 123
result = quote123(value)
fmt.Println(result)
// Output: "123"
result = quote123(func(x int) string { return fmt.Sprintf("%b", x) })
fmt.Println(result)
// Output: "1111011"
foo := func(x int) string { return "foo" }
result = quote123(foo)
fmt.Println(result)
// Output: "foo"
_ = convert(foo) // confirm foo satisfies convert at runtime
// fails due to argument type
// _ = convert(func(x float64) string { return "" })
}
Play: http://play.golang.org/p/XNMtrDUDS0
Tour: https://tour.golang.org/moretypes/25 (Function Closures)
You can pass function as parameter to a Go function. Here is an example of passing function as parameter to another Go function:
package main
import "fmt"
type fn func(int)
func myfn1(i int) {
fmt.Printf("\ni is %v", i)
}
func myfn2(i int) {
fmt.Printf("\ni is %v", i)
}
func test(f fn, val int) {
f(val)
}
func main() {
test(myfn1, 123)
test(myfn2, 321)
}
You can try this out at: https://play.golang.org/p/9mAOUWGp0k
Here is the sample "Map" implementation in Go. Hope this helps!!
func square(num int) int {
return num * num
}
func mapper(f func(int) int, alist []int) []int {
var a = make([]int, len(alist), len(alist))
for index, val := range alist {
a[index] = f(val)
}
return a
}
func main() {
alist := []int{4, 5, 6, 7}
result := mapper(square, alist)
fmt.Println(result)
}
Here is a simple example:
package main
import "fmt"
func plusTwo() (func(v int) (int)) {
return func(v int) (int) {
return v+2
}
}
func plusX(x int) (func(v int) (int)) {
return func(v int) (int) {
return v+x
}
}
func main() {
p := plusTwo()
fmt.Printf("3+2: %d\n", p(3))
px := plusX(3)
fmt.Printf("3+3: %d\n", px(3))
}
This is the simplest way I can come with.
package main
import "fmt"
func main() {
g := greeting
getFunc(g)
}
func getFunc(f func()) {
f()
}
func greeting() {
fmt.Println("Hello")
}
I hope the below example will provide more clarity.
package main
type EmployeeManager struct{
category string
city string
calculateSalary func() int64
}
func NewEmployeeManager() (*EmployeeManager,error){
return &EmployeeManager{
category : "MANAGEMENT",
city : "NY",
calculateSalary: func() int64 {
var calculatedSalary int64
// some formula
return calculatedSalary
},
},nil
}
func (self *EmployeeManager) emWithSalaryCalculation(){
self.calculateSalary = func() int64 {
var calculatedSalary int64
// some new formula
return calculatedSalary
}
}
func updateEmployeeInfo(em EmployeeManager){
// Some code
}
func processEmployee(){
updateEmployeeInfo(struct {
category string
city string
calculateSalary func() int64
}{category: "", city: "", calculateSalary: func() int64 {
var calculatedSalary int64
// some new formula
return calculatedSalary
}})
}
You can also pass the function of a struct, like:
package main
// define struct
type Apple struct {}
// return apple's color
func (Apple) GetColor() string {
return "Red"
}
func main () {
// instantiate
myApple := Apple{}
// put the func in a variable
theFunc := myApple.GetColor
// execute the variable as a function
color := theFunc()
print(color)
}
output will be "Red", check on the playground
Yes Go does accept first-class functions.
See the article "First Class Functions in Go" for useful links.
Related
How do i use the following struct to build a nested json recursively?
The nested struct can go to as many levels as it can.
Both the sample struct and json is mentioned below.
I'm having trouble in building a nested json/object dynamically.
I used the reflect package to access the struct.
I'm able to read through the data but not able to build the same.
type Data struct {
ID string
Name string
Types *Details
}
type Details struct {
Customer int32
Countries int32
}
To:
{
"Name":"Data",
"Fields":[{
"Name":"ID",
"Type":"string"
},
{
"Name":"Name",
"Type":"string"
},
{
"Name":"Details",
"Type":"struct",
"Fields":[{
"Name":"Customer",
"Type":"int32"
},{
"Name":"Countries",
"Type":"int32"
}]
}
And whatever I have done so far, I have attached below:
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func getFields(t reflect.Type, prefix string) {
switch t.Kind() {
case reflect.Ptr:
getFields(t.Elem(), "")
case reflect.Struct:
buildRecursiveFunction(t)
}
}
func buildRecursiveFunction(t reflect.Type) map[string]interface{} {
var jsonArr []interface{}
temp := make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if sf.Name != "state" && sf.Name != "unknownFields" && sf.Name != "sizeCache" {
obj := make(map[string]interface{})
varName := sf.Name
varType := sf.Type.Kind()
obj["Name"] = varName
obj["Type"] = varType.String()
if varType.String() == "ptr" {
obj["Fields"] = buildRecursiveFunction(sf.Type.Elem())
} else {
}
jsonArr = append(jsonArr, obj)
}
}
jsonArrVal, _ := json.Marshal(jsonArr)
fmt.Println(string(jsonArrVal))
return temp
}
func main() {
getFields(reflect.TypeOf(&DeviceEnv{}), "")
// json.Marshal(AllAttributes)
}
Any help is appreciated.
I've hacked around your code and got it (kind of) working here, but it doesn't seem like it's structured in the best way. Better to return and build a recursive object than trying to print it (with a prefix?) as you go.
What about defining a TypeInfo struct and having a recursive function that populates that? I think that leads to a clear structure, and it allows the caller to JSON-marshal the result as they want to:
func main() {
info := MakeTypeInfo("DeviceEnvironment", DeviceEnv{})
b, err := json.MarshalIndent(info, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", b)
}
type TypeInfo struct {
Name string `json:"Name"`
Type string `json:"Type"`
Fields []TypeInfo `json:"Fields,omitempty"`
}
func MakeTypeInfo(name string, value interface{}) TypeInfo {
return makeTypeInfo(name, reflect.TypeOf(value))
}
func makeTypeInfo(name string, t reflect.Type) TypeInfo {
kind := t.Kind()
switch kind {
case reflect.Struct:
var fields []TypeInfo
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fields = append(fields, makeTypeInfo(field.Name, field.Type))
}
return TypeInfo{Name: name, Type: kind.String(), Fields: fields}
case reflect.Pointer:
return makeTypeInfo(name, t.Elem())
default:
return TypeInfo{Name: name, Type: kind.String()}
}
}
Note that I haven't done the field filtering (eg: "unknownFields") that you've shown in your code -- shouldn't be hard to add inside the reflect.Struct case though.
Full example in Go Playground.
I need to read a csv record which has many fields and convert to struct. Currently I am doing as below
//Proclog is type to hold
type Proclog struct {
LogType string `json:"LogType"`
RootLogID string `json:"RootLogId"`
SubLogID string `json:"SubLogId"`
TransactionID string `json:"TransactionId"`
Instance string `json:"Instance"`
Operation string `json:"Operation"`
Status string `json:"Status"`
User string `json:"User"`
Hostname string `json:"Hostname"`
Protocol string `json:"Protocol"`
Target string `json:"Target"`
StartTime string `json:"StartTime"`
ExecuteTime string `json:"ExecuteTime"`
ResponseCode string `json:"ResponseCode"`
FullRequest string `json:"FullRequest"`
FullResponse string `json:"FullResponse"`
}
//ProclogFromCsvRecord convert csv record to LogConsolidation
func ProclogFromCsvRecord(record []string) (*Proclog, error) {
if len(record) != 17 {
return nil, csv.ErrFieldCount
}
return &Proclog{
LogType: record[0],
RootLogID: record[1],
SubLogID: record[2],
TransactionID: record[3],
Instance: record[4],
Operation: record[5],
Status: record[6],
User: record[7],
Hostname: record[8],
Protocol: record[9],
Target: record[10],
StartTime: record[11],
ExecuteTime: record[12],
ResponseCode: record[13],
FullRequest: record[14],
FullResponse: record[15],
}, nil
}
I don't want to hard code the index, is there a better way to convert a slice into struct without hard coding index?
First, I think your code is perfectly fine as you wrote it. Your code must contain the field order in one way or another, and you can't make it much simpler than you did, which is nice.
If anything, you could specify the field order using iota.
type ProclogField int
const (
LogType ProclogField = iota
RootLogID
SubLogID
TransactionID
Instance
Operation
Status
User
Hostname
Protocol
Target
StartTime
ExecuteTime
ResponseCode
FullRequest
FullResponse
ProclogLength
)
Now you can replace the number literals in your code by these constants when creating the Proclog struct.
If you can live without an actual struct, you can define a type directly from the slice, like this:
type Proclog []string
func (p Proclog) Get(f ProclogField) string {
return p[f]
}
func ProclogFromCsvRecord(record []string) (Proclog, error) {
if len(record) != int(ProclogLength)+1 {
return nil, csv.ErrFieldCount
}
return Proclog(record), nil
}
Then, if you need to convert this type to JSON, you can implement the json.Marshaler interface like this:
//go:generate stringer -type=ProclogField
func (p Proclog) MarshalJSON() ([]byte, error) {
m := make(map[string]string)
for z := ProclogField(0); z < ProclogLength; z++ {
m[z.String()] = p.Get(z)
}
return json.Marshal(m)
}
However, this is not what you asked for and I'm not convinced that all of this is better than the simple code you started with.
package main
import (
"log"
"reflect"
"strconv"
)
type People struct {
Name string `mytag:"0"`
Age int `mytag:"1"`
Nunber int64 `mytag:"2"`
}
func FieldsArrayToStruct(p interface{}, values []interface{}) {
rv := reflect.ValueOf(p).Elem()
length := len(values)
for i := 0; i < rv.NumField(); i++ {
tagValue, ok := rv.Type().Field(i).Tag.Lookup("mytag")
typeName := rv.Type().Field(i).Type.Name()
if !ok {
continue
}
if tagValue == "" {
continue
}
index, err := strconv.Atoi(tagValue)
if err != nil || index < 0 || index > length-1 {
continue
}
switch values[i].(type) {
case int:
if typeName == "int" || typeName == "int32" || typeName == "int64" {
rv.FieldByIndex(rv.Type().Field(i).Index).SetInt(int64(values[i].(int)))
}
case string:
if typeName == "string" {
rv.FieldByIndex(rv.Type().Field(i).Index).SetString(values[i].(string))
}
}
}
}
func main() {
var p = &People{}
FilesArrayToStruct(p, []interface{}{"Tom", 19, 11110})
log.Println(p)
}
maybe you need continue finish func FieldsArrayToStruct's switchblock.
Here has an interface SpecificTemplate nested inside struct Template:
package main
import (
"encoding/json"
"fmt"
)
type SpecificTemplate interface {
GetLabel() string
}
type TemplateA struct {
Label string `json:"label"`
}
func (t TemplateA) GetLabel() string {
return t.Label
}
type Template struct {
Id int64 `json:"id"`
SpecificTemplate
}
func main() {
ta := TemplateA{
Label: `label1`,
}
t := Template{
Id: 1,
SpecificTemplate: ta,
}
data, _ := json.Marshal(t)
fmt.Println(string(data))
}
It would be
{
"id":1,
"SpecificTemplate":{
"label":"label1"
}
}
I wanna kown how to show json at same level, just like:
{
"id":1,
"label":"label1"
}
It kinda depends on the level of complexity you wanna reach...
If you want to expose only the label I should advice you to create a MarshalJSON function, just like this...
func (t Template) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Id int64 `json:"id"`
Label string `json:"label"`
}{
Id: t.Id,
Label: t.SpecificTemplate.GetLabel(),
})
}
With this your json.Marshal(t) will call this function and you will receive a flattened json...
However if you want to expose more Fields from the template you should use reflection, as pointed out by Iain Duncan in his comment
I'm trying to understand interfaces in Go. I wrote this:
package main
import "fmt"
type Animal struct {
Name string
Ability string
}
type AbilityShower interface {
ShowAbility() string
}
func (a Animal) ShowAbility() string {
return fmt.Sprintf("%s can %s", a.Name, a.Ability)
}
func main() {
var Dog Animal = Animal{
Name: "Dog",
Ability: "Walk",
}
Dog.ShowAbility()
}
But when I run with go run main.go nothing appears in the console. What am I doing incorrectly?
You are not printing the result. Change the main to
func main() {
var Dog Animal = Animal{
Name: "Dog",
Ability: "Walk",
}
fmt.Println(Dog.ShowAbility())
}
FYI: fmt.Sprintf() returns a string and doesn't print it to standard output
// Sprintf formats according to a format specifier and returns the
resulting string.
You are returning a string from the ShowAbility method, but not outputting it.
package main
import "fmt"
type Animal struct {
Name string
Ability string
}
type AbilityShower interface {
ShowAbility()
}
func (a Animal) ShowAbility() {
fmt.Printf("%s can %s", a.Name, a.Ability)
}
func main() {
var Dog Animal = Animal{
Name: "Dog",
Ability: "Walk",
}
Dog.ShowAbility()
}
Here you are using fmt.Sprintf which actually works like Scanf returning value. You need to assign that value to a variable and print the output.
func main() {
var Dog Animal = Animal{
Name: "Dog",
Ability: "Walk",
}
output := Dog.ShowAbility()
fmt.Println(output)
}
If you wants to print when calling the function ShowAbility() you can use Printf function of fmt package.
func (a Animal) ShowAbility() {
fmt.Printf("%s can %s", a.Name, a.Ability)
}
func main() {
var Dog Animal = Animal{
Name: "Dog",
Ability: "Walk",
}
Dog.ShowAbility()
}
type Struct struct {
Value string `json:"value"`
Value1 string `json:"value_one"`
Nest Nested `json:"nest"`
}
type Nested struct {
Something string `json:"something"`
}
I want to add elements which are not in the structs definitions without creating another struct type. For example
Struct.Extra1 = Nested{"yy"}
Struct.Nested.Extra2 = "zz"
Which will result
{
"Value": "xx",
"Value1": "xx",
"Extra1": {
"Something", "yy"
},
"Nest": {
"Something": "xx",
"Extra2": "zz"
}
}
SOLUTION1:
I thought of adding omitempty to achieve this but it makes the structs complex.
type Struct struct {
Value string
Value1 string
Nest Nested
Extra1 Nested `json:"omitempty"`
}
type Nested struct {
Something string
Extra2 string `json:"omitempty"`
}
SOLUTION2:
myextras := make(map[string]interface{})
// get Struct.Nested in map[string]interface{} format
myextras = Struct.Nest
myextras["Extra2"] = "zz"
// get Struct in map[string]interface{} format
struct["Nest"] = myextras
struct["Extra1"] = Nested{"yy"}
// solves the problem with lots of type casting but doesn't support json tag naming
Is there a better solution to add nested elements which are not represented in struct datatype with json-tag support and could be used to output to user.
If someone is not happy with the solution provided:
Try tidwall/sjson. It provides functions for quick JSON editing without having to define any structure. It saved me a bunch of time yesterday :D
Example usage:
value, _ := sjson.Set(`{"name":{"last":"Anderson"}}`, "name.last", "Smith")
println(value)
// Output:
// {"name":{"last":"Smith"}}
Based on this answer: Can I use MarshalJSON to add arbitrary fields to a json encoding in golang?
You could do something like (demo: http://play.golang.org/p/dDiTwxhoNn):
package main
import (
"encoding/json"
"fmt"
"log"
)
type Book struct {
Title string
Author string
// extra is used for additional dynamic element marshalling
extra func() interface{}
}
type FakeBook Book
func (b *Book) SetExtra(fn func() interface{}) {
b.extra = fn
}
func (b *Book) MarshalJSON() ([]byte, error) {
if b.extra == nil {
b.extra = func() interface{} { return *b }
}
return json.Marshal(b.extra())
}
func main() {
ms := &Book{
Title: "Catch-22",
Author: "Joseph Heller",
}
ms.SetExtra(func() interface{} {
return struct {
FakeBook
Extra1 struct {
Something string `json:"something"`
} `json:"extra1"`
}{
FakeBook: FakeBook(*ms),
Extra1: struct {
Something string `json:"something"`
}{
Something: "yy",
},
}
})
out, err := json.MarshalIndent(ms, "", " ")
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(out))
mb := &Book{
Title: "Vim-go",
Author: "Fatih Arslan",
}
mb.SetExtra(func() interface{} {
return struct {
FakeBook
Something string `json:"something"`
}{
FakeBook: FakeBook(*mb),
Something: "xx",
}
})
out, err = json.MarshalIndent(mb, "", " ")
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(out))
mc := &Book{
Title: "Another-Title",
Author: "Fatih Arslan",
}
out, err = json.MarshalIndent(mc, "", " ")
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(out))
}
yes. there is a type json.Raw which not a struct but []byte. you can manage it out of struct, in any marshal/unmarshal way.
UPDATE
use any way to edit json string on the fly
package main
import (
"fmt"
"encoding/json"
"strings"
)
type Struct struct {
Value string `json:"value"`
Value1 string `json:"value_one"`
Nest json.RawMessage`json:"nest"`
}
func main() {
s := Struct{Value1: "struct string"}
buf, _ := json.Marshal(s)
fmt.Println(string(buf))
s2 := strings.ReplaceAll(string(buf), "null", `{"extra2":{"anykey":3.1415926535}}`)
fmt.Println(s2)
}
https://play.golang.org/p/lOCxJBs5iRJ
The map approach is the only sane way to do it, everything else (like json.RawMessage fields would require an extra marshalling step anyway.
if you don't want install extra package, you can change or add new values with this code :
package main
import "fmt"
import "strings"
import "encoding/json"
func main() {
s := []byte(`{ "level1a":"aaa", "level1b":{"level2a":"bbb", "level2b":{"level3":"aaa"} } }`)
var j interface{}
json.Unmarshal(s, &j)
SetValueInJSON(j, "level1a", "new value 1a")
SetValueInJSON(j, "level1b.level2a", "new value 2a")
SetValueInJSON(j, "level1b.level2b.level3", "new value 3")
SetValueInJSON(j, "level1b.level2c", "new key")
s,_ = json.Marshal(j)
fmt.Println(string(s))
// result: {"level1a":"new value 1a","level1b":{"level2a":"new value 2a","level2b":{"level3":"new value 3"},"level2c":"new key"}}
}
func SetValueInJSON(iface interface{}, path string, value interface{}) interface{} {
m := iface.(map[string]interface{})
split := strings.Split(path, ".")
for k, v := range m {
if strings.EqualFold(k, split[0]) {
if len(split) == 1 {
m[k] = value
return m
}
switch v.(type) {
case map[string]interface{}:
return SetValueInJSON(v, strings.Join(split[1:], "."), value)
default:
return m
}
}
}
// path not found -> create
if len(split) == 1 {
m[split[0]] = value
} else {
newMap := make(map[string]interface{})
newMap[split[len(split)-1]] = value
for i := len(split) - 2; i > 0; i-- {
mTmp := make(map[string]interface{})
mTmp[split[i]] = newMap
newMap = mTmp
}
m[split[0]] = newMap
}
return m
}