{"id":2349,"date":"2021-05-06T16:22:59","date_gmt":"2021-05-06T23:22:59","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=2349"},"modified":"2022-04-10T22:43:49","modified_gmt":"2022-04-11T05:43:49","slug":"the-go-programming-language","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/the-go-programming-language\/","title":{"rendered":"The Go Programming Language"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Golang file server<\/h2>\n\n\n\n<p>Build a simple webserver with a home page, Hello, and a Function   <br>Start by creating a Directory .\\user\\go\\src\\go-server and then open in VSCode<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2022\/03\/image-14.png\" alt=\"\" class=\"wp-image-2607\" width=\"255\" height=\"122\"\/><\/figure>\n\n\n\n<p>Add New File main.go<br>In main.go add code to setup server, The files in the Server will be setup as follows:<a>\u00a0<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2022\/04\/image-1.jpg\" alt=\"\" class=\"wp-image-2625\" width=\"256\" height=\"160\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">In main.go<\/h2>\n\n\n\n<p>Set up file package and import the libraries: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package main\nimport (\u200b\n    \"fmt\"\u200b\n    \"log\"\u200b\n    \"net\/http\"\u200b\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add Functions<\/h3>\n\n\n\n<p>Add functions to handleFunc and handle. The handleFunc will process request for hello and form, the handle will process request with no handle<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func formHandler(w http.ResponseWriter, r *http.Request) {\u200b\n    if err := r.ParseForm(); err != nil {\u200b\n        fmt.Fprintf(w, \"ParseForm() err: %v\", err)\u200b\n        return\u200b\n    }\u200b\n    fmt.Fprintf(w, \"POST request successful\")\u200b\n    name := r.FormValue(\"name\")\u200b\n    address := r.FormValue(\"address\")\u200b\n    fmt.Fprintf(w, \"Name = %s\\n\", name)\u200b\n    fmt.Fprintf(w, \"Address = %s\\n\", address)\u200b\n}\u200b\n\n\nfunc helloHandler(w http.ResponseWriter, r *http.Request) {\u200b\n    if r.URL.Path != \"\/hello\" {\u200b\n        http.Error(w, \"404 not found\", http.StatusNotFound)\u200b\n        return\u200b\n    }\u200b\n    if r.Method != \"GET\" {\u200b\n        http.Error(w, \"method is not supported\", http.StatusNotFound)\u200b\n        return\u200b\n    }\u200b\n    fmt.Fprintf(w, \"hello!\")\u200b\n} \n\nfunc main() {\u200b\n    fileserver := http.FileServer(http.Dir(\".\/static\"))\u200b\n    http.Handle(\"\/\", fileserver)\u200b\n    http.HandleFunc(\"\/form\", formHandler)\n    http.HandleFunc(\"\/hello\", helloHandler)\u200b\n    fmt.Printf(\"Starting server at port 8080\\n\")\u200b\n    if err := http.ListenAndServe(\":8080\", nil); err != nil {\u200b\n        log.Fatal(err)\u200b\n    }\u200b\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add Website html for serving requests<\/h3>\n\n\n\n<p>Add New Folder \\static\\ and Add two html files<br>Static html: will handle request to index.html<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\u200b\n&lt;html&gt;\u200b\n    &lt;head&gt;\u200b\n        &lt;title&gt;\u200b\n            Static Website\u200b\n        &lt;\/title&gt;\u200b\n    &lt;\/head&gt;\u200b\n    &lt;body&gt;\u200b\n        &lt;h2&gt;\u200b\n            Static Website\u200b\n        &lt;\/h2&gt;\u200b\n    &lt;\/body<\/pre>\n\n\n\n<p>form.html: will handle Post Request to form<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\u200b\n&lt;html&gt;\u200b\n    &lt;head&gt;\u200b\n        &lt;meta charset=\"UTF-8\"\/&gt;\u200b\n    &lt;\/head&gt;\u200b\n    &lt;body&gt;\u200b\n        &lt;Div&gt;\u200b\n            &lt;form method=\"POST\" action=\"\/form\"&gt;\u200b\n            &lt;label&gt;Name&lt;\/label&gt;&lt;input name=\"name\" type=\"text\" value=\"\"\/&gt;\u200b\n            &lt;label&gt;Address&lt;\/label&gt;&lt;input name=\"address\" type=\"text\" value=\"\"\/&gt;\u200b\n            &lt;input type=\"submit\" value=\"submit\"\/&gt;\u200b\n        &lt;\/Div&gt;\u200b\n    &lt;\/body&gt;\u200b\n&lt;\/html&gt;<\/pre>\n\n\n\n<p>Hello.html will be generated by handleFunc<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Start FileServer <\/h3>\n\n\n\n<p>on the terminal window type go build to create the binary file to execute<br>once the binary file is created run the file with go main.go<\/p>\n\n\n\n<p>View\/Dowload a copy of FilesServer design in PowerPoint<\/p>\n\n\n\n<iframe src=\"https:\/\/onedrive.live.com\/embed?cid=87DF87EEAD915F91&amp;resid=87DF87EEAD915F91%21148&amp;authkey=ANEuXdQ2msMTB-g&amp;em=2\" style=\"height: 30vmax; width: 64vmax\" frameborder=\"0\" scrolling=\"no\"><\/iframe>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Golang file server Build a simple webserver with a home page, Hello, and a Function Start by creating a Directory .\\user\\go\\src\\go-server and then open in VSCode Add New File main.goIn main.go add code to setup server, The files in the Server will be setup as follows:\u00a0 In main.go Set up file package and import the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/the-go-programming-language\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;The Go Programming Language&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2349","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=2349"}],"version-history":[{"count":18,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2349\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2349\/revisions\/2644"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=2349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}