{"id":73,"date":"2019-03-18T16:38:18","date_gmt":"2019-03-18T16:38:18","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=73"},"modified":"2020-03-09T23:43:34","modified_gmt":"2020-03-09T23:43:34","slug":"interface-to-struct","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/interface-to-struct\/","title":{"rendered":"Interface to struct in Go (Golang)"},"content":{"rendered":"\n

We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of what an empty interface is should read this excellent article: https:\/\/research.swtch.com\/interfaces. <\/p>\n\n\n\n

For conversion of interface{} to a struct, we will use the library – https:\/\/github.com\/mitchellh\/mapstructure<\/a> . Let’s understand how to convert the interface to a struct by an example:<\/p>\n\n\n\n

package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com\/mitchellh\/mapstructure\"\n)\n\ntype NewCustomerEvent struct {\n\tName  string\n\tPhone string\n\tEmail string\n}\n\nfunc main() {\n\tnewCustomer := NewCustomerEvent{Name: \"x\", Phone: \"082213909101\", Email: \"xyz@gmail.com\"}\n\tconvert(newCustomer)\n}\n\nfunc convert(event interface{}) {\n\tc := NewCustomerEvent{}\n\tmapstructure.Decode(event, &c)\n\tfmt.Printf(\"Event is: %v\", c)\n}<\/code><\/pre>\n\n\n\n

Output: <\/strong><\/p>\n\n\n\n

Event is: {x 082213909101 xyz@gmail.com}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[210,3,11,10],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-tech","tag-convert","tag-go","tag-interface","tag-struct"],"yoast_head":"\nInterface to struct in Go (Golang) - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/interface-to-struct\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Interface to struct in Go (Golang) - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/interface-to-struct\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-18T16:38:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-09T23:43:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/interface-to-struct\/\",\"url\":\"https:\/\/golangbyexamples.com\/interface-to-struct\/\",\"name\":\"Interface to struct in Go (Golang) - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2019-03-18T16:38:18+00:00\",\"dateModified\":\"2020-03-09T23:43:34+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/interface-to-struct\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/interface-to-struct\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/interface-to-struct\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interface to struct in Go (Golang)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Interface to struct in Go (Golang) - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/interface-to-struct\/","og_locale":"en_US","og_type":"article","og_title":"Interface to struct in Go (Golang) - Welcome To Golang By Example","og_description":"We come around a situation sometimes in programming where an empty interface might a struct internally and we have to get the concrete struct out of it. Whoever is not aware of...","og_url":"https:\/\/golangbyexamples.com\/interface-to-struct\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2019-03-18T16:38:18+00:00","article_modified_time":"2020-03-09T23:43:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/interface-to-struct\/","url":"https:\/\/golangbyexamples.com\/interface-to-struct\/","name":"Interface to struct in Go (Golang) - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2019-03-18T16:38:18+00:00","dateModified":"2020-03-09T23:43:34+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/interface-to-struct\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/interface-to-struct\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/interface-to-struct\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Interface to struct in Go (Golang)"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-1b","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":7,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/73\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}