Created
December 22, 2023 10:57
-
-
Save BirnadinErick/8d8b3a1df1df766cc9aa58d724a999d1 to your computer and use it in GitHub Desktop.
Go Interfaces Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
type DummyInterface interface { | |
dummyMethod(somethingToOperateOn string) (string, error) | |
} | |
type Example struct { | |
Category string | |
} | |
type Meta struct { | |
Tags []string | |
} | |
func (e Example) dummyMethod(newCategory string) (string, error) { | |
return "Example Method Executed", nil | |
} | |
func (m Meta) dummyMethod(newTag string) (string, error) { | |
return "Meta Method Executed", nil | |
} | |
// a function that will use the interface | |
func demoFunction(d DummyInterface) error { | |
s, err := d.dummyMethod("Sample Input") | |
if err != nil { | |
return errors.New("Something wrong") | |
} | |
fmt.Println(s) | |
return nil | |
} | |
func main() { | |
eg := Example{Category: "Enigma Bits"} | |
if err := demoFunction(eg); err != nil { | |
fmt.Println("Error:", err) | |
} | |
meta := Meta{Tags: []string{"bits", "erick"}} | |
if err := demoFunction(meta); err != nil { | |
fmt.Println("Error:", err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func main() { | |
jsonData := `{"name": "Birnadin", "age": 20}` | |
var result map[string]interface{} | |
if err := json.Unmarshal([]byte(jsonData), &result); err != nil { | |
panic(err) // since it is main() and unrecoverable error | |
} | |
// Using type assertion to get the name | |
if name, ok := result["name"].(string); ok { | |
fmt.Println("Name:", name) | |
} | |
// Using type assertion to get the age | |
if age, ok := result["age"].(float64); // JSON numbers are floats | |
ok { | |
fmt.Println("Age:", age) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func processJSON(input []byte) { | |
var data interface{} | |
if err := json.Unmarshal(input, &data); err != nil { | |
panic(err) | |
} | |
switch v := data.(type) { | |
case []interface{}: | |
fmt.Println("Received an array of objects") | |
for _, obj := range v { | |
processObject(obj) | |
} | |
case map[string]interface{}: | |
fmt.Println("Received a single object") | |
processObject(v) | |
default: | |
fmt.Println("Unknown type") | |
} | |
} | |
func processObject(obj interface{}) { | |
// Process individual object | |
fmt.Println("Processing object:", obj) | |
} | |
func main() { | |
jsonArray := `[{"name": "Birnadin"}, {"name": "Erick"}]` | |
jsonObject := `{"name": "Birnadin Erick"}` | |
fmt.Println("Processing array:") | |
processJSON([]byte(jsonArray)) | |
fmt.Println("\nProcessing single object:") | |
processJSON([]byte(jsonObject)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment