[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-error-handling-unit-testing-all--*":3,"academy-blog-translations-1itryj3ob13xz3z":104},{"data":4,"page":92,"perPage":92,"totalItems":92,"totalPages":92},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":98,"keywords":99,"locale":74,"published_at":100,"scheduled_at":13,"school_blog":96,"short_description":101,"slug":102,"status":94,"title":6,"updated":103,"updated_by":13,"views":97},"Ep.16 Go and Error Handling & Unit Testing - Fixing Errors and Testing Code!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and Error Handling &amp; Unit Testing - Fixing Errors and Testing Code!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">Errors are an inevitable part of programming, and in Go, we have a systematic way to handle them to ensure that the code runs correctly and stably!\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is Error Handling?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Error handling in Go focuses on checking and managing issues that may arise during program execution, such as failed database connections, missing files, or incorrect calculations.\u003C\u002Fp>\u003Cp class=\"p3\">Example of Error Handling in Go :\u003C\u002Fp>\u003Cp class=\"p3\">In Go, we often return an error in functions to check if an error has occurred in the code:\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>The divide function checks if b is 0. If it is 0, it returns an error.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>We use if err != nil to check for errors before using the result.\u003C\u002Fi>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"errors\"\r\n    \"fmt\"\r\n)\r\n\r\nfunc divide(a, b int) (int, error) {\r\n    if b == 0 {\r\n        return 0, errors.New(\"cannot divide by zero\")\r\n    }\r\n    return a \u002F b, nil\r\n}\r\n\r\nfunc main() {\r\n    result, err := divide(10, 0)\r\n    if err != nil {\r\n        fmt.Println(\"Error:\", err)\r\n        return\r\n    }\r\n    fmt.Println(\"Result:\", result)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p1\">\u003Cstrong>Creating Simple Custom Errors\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p1\">In addition to general errors, we can also create our own specific errors using ErrInvalidInput, which helps us differentiate between various types of errors more clearly.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">var ErrInvalidInput = errors.New(\"invalid input provided\")\r\n\r\nfunc checkInput(input int) error {\r\n    if input &lt; 0 {\r\n        return ErrInvalidInput\r\n    }\r\n    return nil\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p1\">\u003Cstrong>Unit Testing in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p1\">Unit testing is the process of testing small parts of a program to ensure they work as expected. In Go, we have the testing package for writing tests.\u003C\u002Fp>\u003Cp class=\"p1\">An example of testing the add function in this code:\u003C\u002Fp>\u003Cp class=\"p1\">\u003Ci>TestAdd is the test function that uses t *testing.T for testing.\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p1\">\u003Ci>t.Errorf is used to report an error if the result does not match the expected outcome.\u003C\u002Fi>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport \"testing\"\r\n\r\nfunc add(a, b int) int {\r\n    return a + b\r\n}\r\n\r\nfunc TestAdd(t *testing.T) {\r\n    result := add(2, 3)\r\n    if result != 5 {\r\n        t.Errorf(\"Expected 5, got %d\", result)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p1\">\u003Cstrong>Table-Driven Tests (Testing Multiple Cases at Once)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p1\">Table-Driven Tests is a technique that allows us to test multiple cases in a single function efficiently.\u003C\u002Fp>\u003Cp class=\"p1\">In this code:\u003C\u002Fp>\u003Cp class=\"p1\">\u003Ci>We create a table with different test cases and then use a for loop to run tests for each case.\u003C\u002Fi>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func TestAddCases(t *testing.T) {\r\n    cases := []struct {\r\n        a, b, expected int\r\n    }{\r\n        {1, 2, 3},\r\n        {2, 3, 5},\r\n        {0, 0, 0},\r\n    }\r\n\r\n    for _, c := range cases {\r\n        result := add(c.a, c.b)\r\n        if result != c.expected {\r\n            t.Errorf(\"Expected %d, got %d\", c.expected, result)\r\n        }\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p1\">\u003Cstrong>Running Unit Tests in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p1\">Use the command go test to run the tests.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">go test\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p1\">\u003Cstrong>In Summary\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li1\">Use errors in functions to check for problems.\u003C\u002Fli>\u003Cli class=\"li1\">Use testing for function testing.\u003C\u002Fli>\u003Cli class=\"li1\">Use Table-Driven Tests to test multiple cases at once.\u003C\u002Fli>\u003C\u002Ful>","2_11zon_7en984s7hm.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ffeydxtjxh0orah3\u002F2_11zon_7en984s7hm.webp","2026-03-04 08:34:34.925Z","",{"keywords":15,"locale":68,"school_blog":78},[16,23,28,33,38,43,48,53,58,63],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:33:57.054Z","g54iztrb8jaqawk","Testing","2026-04-10 16:08:04.159Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:34:15.526Z","m0b6rvfafty5n2x","Error Management","2026-04-10 16:08:10.704Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:33:57.712Z","z6rhic75w1tdqh9","Table-Driven Tests","2026-04-10 16:08:04.322Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:33:54.199Z","tcmhcek6qmvgjns","Unit Testing","2026-04-10 16:08:03.006Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:24:43.639Z","mjcndls2y3h9ob6","Error Handling","2026-04-10 16:07:29.897Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"code":69,"collectionId":70,"collectionName":71,"created":72,"flag":73,"id":74,"is_default":75,"label":76,"updated":77},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":79,"collectionId":80,"collectionName":81,"expand":82,"id":96,"views":97},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":83},{"blogIds":84,"collectionId":85,"collectionName":86,"created":87,"created_by":13,"id":79,"image":88,"image_alt":13,"image_path":89,"label":90,"name":91,"priority":92,"publish_at":93,"scheduled_at":13,"status":94,"updated":95,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":91,"th":91},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","1itryj3ob13xz3z",277,"feydxtjxh0orah3",[20,25,30,35,40,45,50,55,60,65],"2025-01-27 04:36:45.892Z","Learn how to handle errors and test code in Go with effective error handling techniques and Table-Driven testing.","go-error-handling-unit-testing","2026-04-25 02:47:33.241Z",{"en":102}]