{"id":135978,"date":"2024-09-16T10:15:22","date_gmt":"2024-09-16T04:45:22","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135978"},"modified":"2024-09-16T10:15:22","modified_gmt":"2024-09-16T04:45:22","slug":"understand-custom-types","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/","title":{"rendered":"Understand Custom Types"},"content":{"rendered":"\n<p>In addition to the built-in data types provided by Go, you can create your own custom types to better represent specific concepts in your application. These custom types can enhance code readability, maintainability, and type safety.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Custom Types and gRPC<\/strong><\/h2>\n\n\n\n<p>Custom types are particularly useful in the context of gRPC, where you often need to define complex data structures to represent request and response messages. By creating custom types, you can encapsulate related fields and provide meaningful names, improving the overall clarity of your gRPC services.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Custom Types<\/strong><\/h2>\n\n\n\n<p>You can create custom types using the <code>type<\/code> keyword and a descriptive name. Here are some common types you might encounter:<\/p>\n\n\n\n<p><strong>1. Aliases<\/strong><\/p>\n\n\n\n<p>Aliases create a new name for an existing type. This can improve code readability and maintainability.<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Age int\ntype Name string\n<\/code><\/pre>\n\n\n\n<p><strong>2. Structs<\/strong><\/p>\n\n\n\n<p>Structs group together related fields of different types. They are commonly used to represent complex data structures.<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type User struct {\n    ID   string\n    Name string\n    Age  int\n}\n<\/code><\/pre>\n\n\n\n<p><strong>3. Interfaces<\/strong><\/p>\n\n\n\n<p>Interfaces define a set of methods that a type must implement. This allows you to define contracts and create polymorphic behavior.<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Shape interface {\n    Area() float64\n}\n\ntype Rectangle struct {\n    Width  float64\n    Height float64\n}\n\nfunc (r Rectangle) Area() float64 {\n    return r.Width * r.Height &nbsp; \n}\n<\/code><\/pre>\n\n\n\n<p><strong>4. Enumerations<\/strong><\/p>\n\n\n\n<p>Enumerations (enums) define a set of named constants. They are useful for representing a fixed set of values.<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Color int\n\nconst (\n    Red   Color = iota\n    Green Color = iota\n    Blue  Color = iota\n)\n<\/code><\/pre>\n\n\n\n<p><strong>5. Type Assertions<\/strong><\/p>\n\n\n\n<p>Type assertions allow you to check if a value is of a specific type.<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var shape Shape = Rectangle{Width: 5, Height: 10}\n\nif rect, ok := shape.(Rectangle); ok {\n    fmt.Println(\"Area:\", rect.Area())\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Custom Types in gRPC<\/strong><\/h2>\n\n\n\n<p>Custom types can be used to define request and response messages in gRPC. By creating well-defined custom types, you can improve the readability and maintainability of your gRPC services.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Protocol Buffers<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>message User {\n  string id = 1;\n  string name = 2;\n  Age age = 3;\n}\n\nenum Age {\n  UNKNOWN = 0;\n  CHILD = 1;\n  ADULT = 2;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In addition to the built-in data types provided by Go, you can create your own custom types to better represent specific concepts in your application. These custom types can enhance code readability, maintainability, and type safety. Custom Types and gRPC Custom types are particularly useful in the context of gRPC, where you often need to&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135978","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>Understand Custom Types - Tutorial<\/title>\n<meta name=\"description\" content=\"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.\" \/>\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\/understand-custom-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understand Custom Types - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\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\/understand-custom-types\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/\",\"name\":\"Understand Custom Types - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-16T04:45:22+00:00\",\"description\":\"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understand Custom Types\"}]},{\"@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":"Understand Custom Types - Tutorial","description":"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.","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\/understand-custom-types\/","og_locale":"en_US","og_type":"article","og_title":"Understand Custom Types - Tutorial","og_description":"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/","name":"Understand Custom Types - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-16T04:45:22+00:00","description":"Explore custom types in Go to define and use your own data types. Learn how to create type aliases, define new types, and more.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/understand-custom-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Understand Custom Types"}]},{"@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\/135978","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\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135978"}],"version-history":[{"count":1,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135978\/revisions"}],"predecessor-version":[{"id":135981,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135978\/revisions\/135981"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}