Skip to content

Instantly share code, notes, and snippets.

@davidbegin
Created August 6, 2020 17:02
Show Gist options
  • Save davidbegin/cb246ea016b2e5020865002e6346a48d to your computer and use it in GitHub Desktop.
Save davidbegin/cb246ea016b2e5020865002e6346a48d to your computer and use it in GitHub Desktop.
# Teaching Interfaces to an ADHD monster
domalix: hate is a strong word!
domalix: im doing great :) how about you?
domalix: I don't think generic interfaces are the wrong thing to do necessarily, the linked list library uses empty interfaces to store the data
domalix: and i guess you'd have to type assert once you get the data back
domalix: I think the interface stuff i golang is the hardest stuff to learn, someday I'll understand it too... maybe...
domalix: maybe look at the linked list library for inspiration?
domalix: container/list
domalix: type Element struct { // The value stored with this element. Value interface{} // contains filtered or unexported fields } thats how the linked list stores its values in go
domalix: gahhhh that doesn't come out nicely
domalix: you can make type assertions in switch statements, so clearly it wasn't completley unintended right
domalix: the linked list has a struct element, and the only thing inside the element is an empty interface
domalix: so you can do everyything with element in al lthe code, but once it comes to reading the value i guess you'd have to do type assertion then
domalix: c[value].(User)
domalix: hrm, its because you chagned it off the map[stirng]interface{}
domalix: i think what erik is trying to say, is you can return the map[string]interface(you got rid of all that). It likely wasnt working before because when you pulled the value out of the map[string] portion you didnt type assert on the interface inside the map[string]interface
domalix: otherwise you're storing the map as an inteface, so you have to assert out the map[string]intrface first, then you're asserting the value out of the map, so its like an extra step you don't nee dto do
domalix: Can I post a link?
domalix: Its to some golang source
domalix: https://golang.org/src/container/list/list.go
domalix: seeing how that is implemente dmay hepl, and its pretty simple
domalix: Sometimes when you're learning its better to just walk away and then come back later with a fresh look
domalix: so maybe that'll help anyways
domalix: Yea, this is a weird example because its just a list, but they're storing a generic value in them
domalix: ^
domalix: yup, and sorry if that example isn't great, but its an example of where they used a generic value in the standard library
domalix: I think the idea, is to work in generics for as long as you can in any library you create, and leave the assertion to whatever code i susing said libraries
domalix: by generics, i mean empty interfaces
domalix: Thanks bald :)
domalix: I think the empty interface is just a weird concept, even in go terms, an interface is supposed to define some behavior but the empty interface defines nothing, but also implement everything, so its just odd
domalix: Maybe something better to look up is golang empty interfaces, but its more specific
domalix: oohhh this is good erik
domalix: this is the type of stuff you see in the standard library
domalix: I think the important part is the WorkItem with the DoWork function, so every different type you might use woul dhave to implement DoWork, but then it would work no matter what, without type asserting everywhere
domalix: thats really good stuff :>
domalix: brain fuzzeddd
domalix: No wayyyyy
domalix: good to see someone tripping up over things like I would
domalix: tbh
domalix: Trivia interviews are sooooo bad
domalix: The point of the tough problems at FANG companies is to see how you think, no on cares what you know. If you know how to program in one language, you know how to learn to program in them all
domalix: My BS in CS is so good god..... base level understanding of computers
domalix: that stuff is so valuable
domalix: the computer architecture stuff, the nuts and bolts
domalix: also, building the BNF for languages was really neat to do in college
domalix: LA prices in the midwest is so crazy
domalix: @james_dot_js lower it to what, if you don't mind me asking
domalix: Honestly, i learned so much just doing a personal projects..... it cemented everything i learned in college(where i had a hard time learning it)
domalix: I like the idea of passion projects
domalix: @erikdotdev im pretty sure google did studies on their hires and they showed theres no correlation between a degree and success
domalix: @erikdotdev so your story makes sense :)
domalix: Ill say though, the degree does help give you foundational knowledge, bu tnone of it is super real world
domalix: and its broad foundational knowledge
domalix: My problem with setting down to do a project...... I like learning the nitty gritty, and if you try to do that in programming you'll never get anything done
domalix: the rabbit hole is too deep everywhere
domalix: schooling is worth what you take out of it
domalix: im glad i got my bs
domalix: my knowledge would have been focused
domalix: but now I have interest in electrical engneering and a few other spread out things, it allows me to lean on some stuff i've "heard of" and go from there
domalix: its just a boader set of knowledge, forced on you
domalix: ive never programmed for a living, but man me and a few of my friends love talking code
domalix: Yea but at college you have to learn terrible things like Calculus 2
domalix: :p
domalix: LOL
domalix: sad
domalix: Type assertion is just telling the compiler to treat an interface like another type. During runtime it asserts it, panics if it realizes its not actually that type, otherwise lets you do what you gotta do
domalix: Yea, which makes sense. A concrete type implements an interface, but a concrete type doesn't implement another concrete type.
domalix: i swear i just read something about interfaces that descrived this well
domalix: let me see if I can find this
domalix: I think there is this idea of dynamic and static types in golang, so even though something is stored in an interface, which is a different.... dynamic type? the underlying static type is kept track of, so you can assert it I guess, so the dynamic type can change but the static type can't. But the static type contains the members you have access too(this is my understanding, please don't hate me if I'm wrong)
domalix: still looking for the documentation I found
domalix: rather, dynamica type contains the members you have access to
domalix: Have you seen the reflect package?
domalix: I think you can reflect the types of the values, which might help you understand whats going on in the code that is blowing up
domalix: basically just ask the compiler to print the name of the type
domalix: https://medium.com/golangspec/interfaces-in-go-part-i-4ae53a97479c This seems really good, about halfway down there is Interface type value, I think you already have a good grasp of everything above that portion
domalix: I can imagine some don't like it becuase you could have two interfaces with the same methodset that do different things, but you implicitly meet both even if one would undefined behavior for your data
domalix: Yea erik, it could just be used for the wrong thing potentially, so implicitly its just weird - how you'd end up in that situation im not sure
domalix: I think its a stretch, but ya
domalix: Thats interesting, i havent done enough interface stuff, but interfaces that require other interfaces, good to know
domalix: This is why everyone should blog, this blog about interfaces seems really good
domalix: I don't blog, but watching this stream im kinda inspired to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment