{"id":120021,"date":"2023-04-14T18:31:52","date_gmt":"2023-04-14T13:01:52","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=120021"},"modified":"2024-04-12T14:32:32","modified_gmt":"2024-04-12T09:02:32","slug":"assert-in-golang","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/","title":{"rendered":"Assert in Golang"},"content":{"rendered":"\n<p>In Golang, assertion is the process of verifying that a value belongs to a specific type. It is used to check if an interface value holds a specific type or not. The syntax for an assertion is:<\/p>\n\n\n\n<p>value, ok := interface_value.(type_name)<\/p>\n\n\n\n<p>Here, value is the value of the interface type, ok is a boolean value that indicates whether the assertion was successful or not, and type_name is the type that you want to assert. If the assertion is successful, the value of the interface is returned as value, and ok is set to true. Otherwise, value is set to the zero value of the type, and ok is set to false.<\/p>\n\n\n\n<p>For example, consider the following code:<\/p>\n\n\n\n<p>var i interface{} = &#8220;hello&#8221;<br>s, ok := i.(string)<\/p>\n\n\n\n<p>In this example, we have declared an interface variable i and initialized it with a string value &#8220;hello&#8221;. We then assert that i is a string type and assign the result to the variables s and ok. Since i is a string, s will be assigned the value &#8220;hello&#8221; and ok will be true.<\/p>\n\n\n\n<p>If the assertion fails, then ok will be false and value will be set to the zero value of the type. For example, consider the following code:<\/p>\n\n\n\n<p>var i interface{} = 10<br>s, ok := i.(string)<\/p>\n\n\n\n<p>In this case, the assertion fails because i is not a string type. Therefore, s will be assigned an empty string, which is the zero value of the string type, and ok will be false.<\/p>\n\n\n\n<h3 class=\"has-text-align-center has-content-primary-background-color has-background wp-block-heading\"><strong>Practice Questions on assertion in Golang<\/strong><\/h3>\n\n\n\n<p>Here are some practice questions on assertion in Golang:<\/p>\n\n\n\n<p><strong>What is the output of the following Go code?<\/strong><\/p>\n\n\n\n<p>var i interface{} = 10<br>s, ok := i.(string)<br>fmt.Println(s, ok)<\/p>\n\n\n\n<p>A. 10 true<br>B. &#8220;&#8221; false<br>C. 0 false<br>D. panic<\/p>\n\n\n\n<p>Answer: B<\/p>\n\n\n\n<p><strong>Which keyword is used to perform an assertion in Golang?<\/strong><\/p>\n\n\n\n<p>A. assert<br>B. check<br>C. verify<br>D. type<\/p>\n\n\n\n<p>Answer: D<\/p>\n\n\n\n<p><strong>What is the syntax for asserting that an interface value holds a slice of integers?<\/strong><\/p>\n\n\n\n<p>A. value, ok := interface_value.(int)<br>B. value, ok := interface_value.([]int)<br>C. value, ok := interface_value.(string)<br>D. value, ok := interface_value.(float64)<\/p>\n\n\n\n<p>Answer: B<\/p>\n\n\n\n<p><strong>What is the output of the following Go code?<\/strong><\/p>\n\n\n\n<p>var i interface{} = []int{1, 2, 3}<br>s, ok := i.(string)<br>fmt.Println(s, ok)<\/p>\n\n\n\n<p>A. [1 2 3] true<br>B. [] false<br>C. 0 false<br>D. panic<\/p>\n\n\n\n<p>Answer: B<\/p>\n\n\n\n<p><strong>What is the output of the following Go code?<\/strong><\/p>\n\n\n\n<p>var i interface{} = &#8220;hello&#8221;<br>s := i.(string)<br>fmt.Println(s)<\/p>\n\n\n\n<p>A. hello<br>B. &#8220;&#8221;<br>C. panic<br>D. nil<\/p>\n\n\n\n<p>Answer: A<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Golang, assertion is the process of verifying that a value belongs to a specific type. It is used to check if an interface value holds a specific type or not. The syntax for an assertion is: value, ok := interface_value.(type_name) Here, value is the value of the interface type, ok is a boolean value&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-120021","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Assert in Golang - Tutorial<\/title>\n<meta name=\"description\" content=\"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Assert in Golang - Tutorial\" \/>\n<meta property=\"og:description\" content=\"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T09:02:32+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/\",\"name\":\"Assert in Golang - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2023-04-14T13:01:52+00:00\",\"dateModified\":\"2024-04-12T09:02:32+00:00\",\"description\":\"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Assert in Golang\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Assert in Golang - Tutorial","description":"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!","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:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/","og_locale":"en_US","og_type":"article","og_title":"Assert in Golang - Tutorial","og_description":"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T09:02:32+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/","name":"Assert in Golang - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2023-04-14T13:01:52+00:00","dateModified":"2024-04-12T09:02:32+00:00","description":"What is Assert in Golang? Know the concept of Assert in Golang and start your learning and certification journey now with vskills!","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/assert-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Assert in Golang"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/120021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=120021"}],"version-history":[{"count":6,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/120021\/revisions"}],"predecessor-version":[{"id":120430,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/120021\/revisions\/120430"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=120021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=120021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=120021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}