Comments (5)
@hendrasan you are correct. The example has been updated. Thanks!
from golang-for-nodejs-developers.
Is there a Golang equivalent of this:
let person = {
name: "test",
age: 13
}
let student = {
...person,
school: "harvard"
}
console.log(student)
/*
Object {
age: 13,
name: "test",
school: "harvard"
}
*/
The goal is to pass attributes of any Go struct into a new struct with an extra attribute.
from golang-for-nodejs-developers.
@ToJen this is the closest thing I think
package main
import "fmt"
func main() {
person := map[string]interface{}{
"name": "test",
"age": 13,
"school": "stanford",
}
student := map[string]interface{}{
"school": "harvard",
}
for k, v := range person {
if _, ok := student[k]; !ok {
student[k] = v
}
}
fmt.Println(student) // map[age:13 name:test school:harvard]
}
https://play.golang.org/p/BlVCIjH93vA
from golang-for-nodejs-developers.
Thanks, the use case is more like this:
type TypeA struct {
name string
age int
}
type TypeB struct {
id string
capacity int
}
type TypeC struct {
// accept any other struct's fields
school string
}
from golang-for-nodejs-developers.
@ToJen I believe this is what you're looking for:
package main
import "fmt"
type TypeA struct {
name string
age int
}
type TypeB struct {
id string
capacity int
}
type TypeC struct {
TypeA
TypeB
school string
}
func main() {
typeA := TypeA{
name: "alice",
age: 18,
}
typeB := TypeB{
id: "a123",
capacity: 1,
}
typeC := TypeC{
TypeA: typeA,
TypeB: typeB,
school: "harvard",
}
fmt.Println(typeC.name) // "alice"
fmt.Println(typeC.age) // 18
fmt.Println(typeC.id) // "a123"
fmt.Println(typeC.capacity) // 1
fmt.Println(typeC.school) // "harvard"
}
https://play.golang.org/p/jhZe_HjGXja
from golang-for-nodejs-developers.
Related Issues (5)
- color in gopher's eyes HOT 1
- deleted
- Comparison of Node npm and Go dep HOT 2
- Promises example HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from golang-for-nodejs-developers.