Giter Club home page Giter Club logo

Comments (5)

miguelmota avatar miguelmota commented on May 17, 2024

@hendrasan you are correct. The example has been updated. Thanks!

from golang-for-nodejs-developers.

ToJen avatar ToJen commented on May 17, 2024

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.

miguelmota avatar miguelmota commented on May 17, 2024

@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.

ToJen avatar ToJen commented on May 17, 2024

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.

miguelmota avatar miguelmota commented on May 17, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.