{"id":392889,"date":"2024-06-29T10:40:20","date_gmt":"2024-06-29T10:40:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=392889"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=392889","title":{"rendered":"<span>Go Quiz<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this series, we will be discussing interesting aspects and corner cases of Golang. Some questions will be obvious, and some will require a closer look even from an experienced Go developer. These question will help to deeper the understanding of the programming language, and its underlying philosophy. Without much ado, let&#8217;s start with the first part.<\/p>\n<p>  <\/p>\n<h2 id=\"value-assignment\">Value assignment<\/h2>\n<p>  <\/p>\n<p>What value <code>y<\/code> will have at the end of the execution?<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">func main() {     var y int     for y, z := 1, 1; y &lt; 10; y++ {         _ = y         _ = z     }     fmt.Println(y) } <\/code><\/pre>\n<p>  <\/p>\n<p>According to the specification, <a name=\"habracut\"><\/a> <code>for<\/code> loop creates its own scope. Therefore, we are dealing with two different scopes there: one inside the <code>main<\/code> function, and one inside the <code>for<\/code> loop. Therefore, we don&#8217;t reassign <code>y<\/code> inside the <code>for<\/code> loop initialization, but instead creating new <code>y<\/code> that shadows the one from the outer scope. Therefore, the outer <code>y<\/code> is not affected, and the program will output <code>0<\/code>.<\/p>\n<p>  <\/p>\n<h2 id=\"a-part-of-a-string\">A part of a string<\/h2>\n<p>  <\/p>\n<p>In this example, we have a string and would like to access a part of it. What would be the result of the following snippet?<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">s := \"9\"  v1 := s[0]  for _, v2 := range s {     fmt.Println(v1)     fmt.Println(v2)     fmt.Println(v1 == v2)     break \/\/ a single loop iteration }<\/code><\/pre>\n<p>  <\/p>\n<p>The first two print statement would output the same result. A string in Golang is an immutable array of bytes and every character is encoded in UTF-8. In this case, we are dealing with the ASCII-only string, therefore, character <code>9<\/code> will be encoded as a single byte with the value equal to <code>57<\/code>. Therefore, the first print statement would output <code>57<\/code>. Exactly the same value would be printed at the second line, as in this case, we will have rune <code>r<\/code> that consists of a single byte.<\/p>\n<p>  <\/p>\n<p>However, the program won&#8217;t compile due to the third line, as we are dealing with different types: uint8 (under alias <code>byte<\/code>) and int32 (under alias <code>rune<\/code>). The numeric value of the variables is equal, but their types are different, therefore, they cannot be compared without the explicit type conversion.<\/p>\n<p>  <\/p>\n<h2 id=\"struct-conversion\">Struct Conversion<\/h2>\n<p>  <\/p>\n<p>In this example, we have two similar structs that differ only in struct tags. Such an approach could be used in a real life. For example, you can have a separate representation of a single domain model in different packages: package <code>db<\/code> that is responsible for database persistence and package <code>api<\/code> that is responsible for handling the incoming requests. In this case, the structs would be equal save for the struct tags. What would be the result of the following code snippet? <code>#v<\/code> outputs the full Golang representation of the value, including the type of the struct and its field names.<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">type Struct1 struct {     A int `db:\"a\"` }  type Struct2 struct {     A int `json:\"a\"` }  func main() {     s1 := Struct1{A: 1}     s2 := Struct2(s1)     fmt.Printf(\"%#v\", s2) }<\/code><\/pre>\n<p>  <\/p>\n<p>That&#8217;s a tricky question because according to the Golang specification a struct tag is a part of the struct definition. Therefore, at some point, it wasn&#8217;t possible to do the conversion. However, later the Go team decided to relax the constraint (without changing the definition of the struct in the spec), and now such conversion is permitted.<\/p>\n<p>  <\/p>\n<p><code>main.Struct2{A:1}<\/code><\/p>\n<p>  <\/p>\n<p>How about this snippet? We are trying to convert <code>Struct1<\/code> to <code>Struct2<\/code>. All information necessary for <code>Struct2<\/code> is available in <code>Struct1<\/code>. However, there is also a redundant field <code>B<\/code> in <code>Struct1<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">type Struct1 struct {     A int     B int }  type Struct2 struct {     A int }  func main() {     s1 := Struct1{}     s2 := Struct2(s1)     fmt.Printf(\"%#v\", s2) }<\/code><\/pre>\n<p>  <\/p>\n<p>In this case, the specification does not care whether we have all the information to instantiate <code>Struct2<\/code> from <code>Struct1<\/code>. <code>Struct1<\/code> has an extra field, and that&#8217;s the end of the deal: the operation is not permitted, and the code won&#8217;t compile.<\/p>\n<p>  <\/p>\n<h2 id=\"json-unmarshalling\">JSON Unmarshalling<\/h2>\n<p>  <\/p>\n<p>Will the existing records in the map be preserved when we unmarshal JSON-encoded values into it? What happens in the case of a collision (note key <code>Field1<\/code>) ?<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">s := map[string]int{         \"Field1\": 1,         \"Field2\": 2, }  data := `{\"Field2\": 202}`  err := json.Unmarshal([]byte(data), &amp;s) if err != nil {     panic(err) } fmt.Println(s)<\/code><\/pre>\n<p>  <\/p>\n<p>Existing records in the map will be preserved. In the case of a collision, the value will be overwritten.<\/p>\n<p>  <\/p>\n<p><code>map[Field1:1 Field2:202]<\/code><\/p>\n<p>  <\/p>\n<p>What about structs? <\/p>\n<p>  <\/p>\n<pre><code class=\"go\">type request struct {     Field1, Field2 int } r := request{Field1: 1, Field2: 2}  data := `{\"Field2\": 202}`  err := json.Unmarshal([]byte(data), &amp;r) if err != nil {     panic(err) } fmt.Println(r)<\/code><\/pre>\n<p>  <\/p>\n<p>The same logic is valid here:<\/p>\n<p>  <\/p>\n<p><code>{Field1:1 Field2:202}<\/code><\/p>\n<p>  <\/p>\n<p>And that all the question for today \ud83d\ude42 How many right answers did you get out of four?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/550786\/\"> https:\/\/habr.com\/ru\/articles\/550786\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this series, we will be discussing interesting aspects and corner cases of Golang. Some questions will be obvious, and some will require a closer look even from an experienced Go developer. These question will help to deeper the understanding of the programming language, and its underlying philosophy. Without much ado, let&#8217;s start with the first part.<\/p>\n<p>  <\/p>\n<h2 id=\"value-assignment\">Value assignment<\/h2>\n<p>  <\/p>\n<p>What value <code>y<\/code> will have at the end of the execution?<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">func main() {     var y int     for y, z := 1, 1; y &lt; 10; y++ {         _ = y         _ = z     }     fmt.Println(y) } <\/code><\/pre>\n<p>  <\/p>\n<p>According to the specification, <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-392889","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/392889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=392889"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/392889\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}