{"id":278,"date":"2024-09-15T17:15:48","date_gmt":"2024-09-15T10:15:48","guid":{"rendered":"https:\/\/gotutorial.org\/?page_id=278"},"modified":"2024-09-15T17:15:49","modified_gmt":"2024-09-15T10:15:49","slug":"go-methods","status":"publish","type":"page","link":"https:\/\/www.gotutorial.org\/go-tutorial\/go-methods\/","title":{"rendered":"Go Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about Go methods, which allow you to define methods on types including structs.<\/p>\n\n\n\n<p>Go methods allow you to define functions on types including <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-struct\/\">structs<\/a>. By definition, Go methods are <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-functions\/\">functions<\/a> with a receiver argument.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for defining a method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r receiverType)<\/span> <span class=\"hljs-title\">methodName<\/span><span class=\"hljs-params\">(parameters)<\/span> <span class=\"hljs-title\">returnType<\/span><\/span> {\n    <span class=\"hljs-comment\">\/\/ method body<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you place the receiver argument between the <code>func<\/code> keyword and method name. The rest is the same as the syntax for <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-functions\/\">defining a function<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go method example<\/h2>\n\n\n\n<p>Step 1. Define a <code>Rectangle<\/code> struct that has <code>length<\/code> and <code>width<\/code> fields:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">type<\/span> Rectangle <span class=\"hljs-keyword\">struct<\/span> {\n    length <span class=\"hljs-keyword\">float64<\/span>\n    width  <span class=\"hljs-keyword\">float64<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 2. Create a function that calculates the area of a <code>Rectangle<\/code> struct:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">Area<\/span><span class=\"hljs-params\">(r Rectangle)<\/span> <span class=\"hljs-title\">float64<\/span><\/span> {\n    <span class=\"hljs-keyword\">return<\/span> r.length * r.width\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 3. Create a <code>Rectangle<\/code> struct value, calculate the area, and display the result on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    r := Rectangle{length: <span class=\"hljs-number\">10<\/span>, width: <span class=\"hljs-number\">5<\/span>}\n    fmt.Println(<span class=\"hljs-string\">\"Area of rectangle is\"<\/span>, Area(r))\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-keyword\">type<\/span> Rectangle <span class=\"hljs-keyword\">struct<\/span> {\n    length <span class=\"hljs-keyword\">float64<\/span>\n    width  <span class=\"hljs-keyword\">float64<\/span>\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">Area<\/span><span class=\"hljs-params\">(r Rectangle)<\/span> <span class=\"hljs-title\">float64<\/span><\/span> {\n    <span class=\"hljs-keyword\">return<\/span> r.length * r.width\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    r := Rectangle{length: <span class=\"hljs-number\">10<\/span>, width: <span class=\"hljs-number\">5<\/span>}\n    fmt.Println(<span class=\"hljs-string\">\"Area of rectangle is\"<\/span>, Area(r))\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">Area of rectangle is <span class=\"hljs-number\">50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The program works fine. But it&#8217;s more intuitive if we can call the <code>Area()<\/code> function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">r.Area()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To do this, we can define the <code>Area()<\/code> function as a method of the <code>Rectangle<\/code> struct type.<\/p>\n\n\n\n<p>Step 4. Change the <code>Area()<\/code> function to a method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r Rectangle)<\/span> <span class=\"hljs-title\">Area<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">float64<\/span><\/span> {\n    <span class=\"hljs-keyword\">return<\/span> r.length * r.width\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the receiver argument<code> (r Rectangle)<\/code> between the <code>func<\/code> keyword and <code>Area<\/code> method name.<\/li>\n\n\n\n<li>Second, remove parameters from the <code>Area()<\/code> function because we can access the <code>length<\/code> and <code>width<\/code> field via the <code>r<\/code> parameter.<\/li>\n<\/ul>\n\n\n\n<p>The <code>Area()<\/code> function becomes a method of the <code>Rectangle<\/code> struct type.<\/p>\n\n\n\n<p>Here&#8217;s the updated program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-keyword\">type<\/span> Rectangle <span class=\"hljs-keyword\">struct<\/span> {\n    length <span class=\"hljs-keyword\">float64<\/span>\n    width  <span class=\"hljs-keyword\">float64<\/span>\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r Rectangle)<\/span> <span class=\"hljs-title\">Area<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">float64<\/span><\/span> {\n    <span class=\"hljs-keyword\">return<\/span> r.length * r.width\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    r := Rectangle{length: <span class=\"hljs-number\">10<\/span>, width: <span class=\"hljs-number\">5<\/span>}\n    fmt.Println(<span class=\"hljs-string\">\"Area of rectangle is\"<\/span>, r.Area())\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Define receivers as pointers<\/h2>\n\n\n\n<p>Like functions, methods use the <em><a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-struct\/\">pass-by-value<\/a><\/em> mechanism. This means that methods copy arguments and work on the copied versions inside their body. Any changes methods make to the arguments will not be reflected in the original arguments.<\/p>\n\n\n\n<p>Therefore, if you want to change the fields of a struct inside methods, you need to use a <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-pointers\/\">pointer<\/a> to the struct as the receiver.<\/p>\n\n\n\n<p>For example, the following defines the <code>Scale()<\/code> method of the <code>Rectangle<\/code> struct. It uses a pointer as a receiver:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r *Rectangle)<\/span> <span class=\"hljs-title\">Scale<\/span><span class=\"hljs-params\">(factor <span class=\"hljs-keyword\">float64<\/span>)<\/span><\/span> {\n    r.length *= factor\n    r.width *= factor\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>Scale<\/code> method changes both the <code>length<\/code> and <code>width<\/code> fields of the rectangle. You may wonder why we can access them using this syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">r.length\nr.width<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But not using the correct syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">(*r).length\n(*r).width<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is possible due to the <em>automatic dereferencing<\/em> feature in GO.<\/p>\n\n\n\n<p>Go allows you to access struct fields through a pointer without explicitly dereferencing the pointer. <\/p>\n\n\n\n<p>When the Go compiler compiles the code, it will automatically convert <code>r.width<\/code> to <code>(*r).width<\/code> for you, making the syntax more convenient and easier to read.<\/p>\n\n\n\n<p>Here&#8217;s the updated program that includes the <code>Scale()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-keyword\">type<\/span> Rectangle <span class=\"hljs-keyword\">struct<\/span> {\n    length <span class=\"hljs-keyword\">float64<\/span>\n    width  <span class=\"hljs-keyword\">float64<\/span>\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r Rectangle)<\/span> <span class=\"hljs-title\">Area<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">float64<\/span><\/span> {\n    <span class=\"hljs-keyword\">return<\/span> r.length * r.width\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-params\">(r *Rectangle)<\/span> <span class=\"hljs-title\">Scale<\/span><span class=\"hljs-params\">(factor <span class=\"hljs-keyword\">float64<\/span>)<\/span><\/span> {\n    r.length *= factor\n    r.width *= factor\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    r := Rectangle{length: <span class=\"hljs-number\">10<\/span>, width: <span class=\"hljs-number\">5<\/span>}\n    r.Scale(<span class=\"hljs-number\">10<\/span>)\n    fmt.Println(r)\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">{100 50}<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Methods are functions with a receiver argument.<\/li>\n\n\n\n<li>Use a pointer to the struct as a receiver to modify the struct fields inside methods.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t<div class=\"wth-thumbs\">\n\t\t<button\n\t\t\tdata-post=\"278\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-methods\/\"\n\t\t\tdata-post-title=\"Go Methods\"\n\t\t\tdata-response=\"1\"\n\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t<\/button>\n\n\t\t<button\n\t\t\tdata-response=\"0\"\n\t\t\tdata-post=\"278\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-methods\/\"\n\t\t\tdata-post-title=\"Go Methods\"\n\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t<\/button>\n\t<\/div>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about Go methods, which allow you to define methods on types including structs.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":10,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-278","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":3,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/278\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/278\/revisions\/281"}],"up":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/media?parent=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}