{"id":135946,"date":"2024-09-16T10:13:45","date_gmt":"2024-09-16T04:43:45","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135946"},"modified":"2024-09-16T10:13:46","modified_gmt":"2024-09-16T04:43:46","slug":"working-with-structs","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/","title":{"rendered":"Working with Structs"},"content":{"rendered":"\n<p>Structs in Go are user-defined data types that group together related fields. They provide a way to create complex data structures and organize your code. In the context of gRPC, they are often used to define request and response messages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Structs<\/strong><\/h2>\n\n\n\n<p>To create a struct, you use the <code class=\"\">type<\/code> keyword followed by the struct name and a list of fields:<\/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<h2 class=\"wp-block-heading\"><strong>Accessing Fields<\/strong><\/h2>\n\n\n\n<p>You can access the fields of a struct using the dot notation:<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>user := User{\n    ID:   \"user1\",\n    Name: \"Alice\",\n    Age:  30,\n}\n\nfmt.Println(user.Name) \/\/ Output: Alice\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Modifying Fields<\/strong><\/h2>\n\n\n\n<p>You can modify the fields of a struct directly:<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>user.Age = 31\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Struct Methods<\/strong><\/h2>\n\n\n\n<p>You can define methods on structs to provide custom behavior:<\/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\nfunc (u User) Greet() string {\n    return \"Hello, \" + u.Name + \"!\"\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Structs and gRPC<\/strong><\/h2>\n\n\n\n<p>Structs are commonly used in gRPC to define request and response messages. You can create structs to represent complex data structures and pass them between client and server.<\/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  int32 age = 3;\n}\n<\/code><\/pre>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type userService struct{}\n\nfunc (s *userService) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.User, error) {\n  user := &amp;pb.User{\n    Id:   \"user1\",\n    Name: \"Alice\",\n    Age:  30,\n  }\n  return user, nil\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Struct Embedding<\/strong><\/h2>\n\n\n\n<p>You can embed one struct within another to create hierarchical data structures:<\/p>\n\n\n\n<p>Go<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Address struct {\n    Street string\n    City   string\n    State  string\n}\n\ntype Person struct {\n    Name   string\n    Age    int\n    Address Address\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Structs in Go are user-defined data types that group together related fields. They provide a way to create complex data structures and organize your code. In the context of gRPC, they are often used to define request and response messages. Creating Structs To create a struct, you use the type keyword followed by the struct&#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-135946","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>Working with Structs - Tutorial<\/title>\n<meta name=\"description\" content=\"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.\" \/>\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\/working-with-structs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with Structs - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/\" \/>\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-09-16T04:43:46+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/\",\"name\":\"Working with Structs - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-16T04:43:45+00:00\",\"dateModified\":\"2024-09-16T04:43:46+00:00\",\"description\":\"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Working with Structs\"}]},{\"@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":"Working with Structs - Tutorial","description":"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.","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\/working-with-structs\/","og_locale":"en_US","og_type":"article","og_title":"Working with Structs - Tutorial","og_description":"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-09-16T04:43:46+00:00","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/","name":"Working with Structs - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-16T04:43:45+00:00","dateModified":"2024-09-16T04:43:46+00:00","description":"Learn about working with structs in Go to create and manage complex data types. Explore how to define, instantiate, and manipulate structs.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/working-with-structs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Working with Structs"}]},{"@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\/135946","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=135946"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135946\/revisions"}],"predecessor-version":[{"id":135980,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135946\/revisions\/135980"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}