create account

My first experience running a Go web server by codero

View this thread on: hive.blogpeakd.comecency.com
· @codero · (edited)
$0.04
My first experience running a Go web server
Today, I started a code along with an online tutorial that walks me through creating a web app with go. Before everything else though, I needed to install the go environment (mac version). Following the completion of the installation process, I  set up the directory where my app would reside. If you are on a mac, a go directory is created on your home directory by default. There you will be able to create all your go apps (it will serve as your go workspace).

![golang.png](https://steemitimages.com/DQmdkSjpHyyeXwNQNWbSaCV4AcPJfveEgjCLEnFrzfQEf7H/golang.png)



 Go into the `/go` directory and create a  `src` directory. Under `src` create a directory named  `myapp` and inside create the file `main.go`.

```
/go
  /src
   /myapp
     - main.go
```




The`main.go` file is where you will write your go code in.

```
package main

import "net/http"

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hey!"))
	})

	http.ListenAndServe(":8000", nil)
}

```

You can see in the above I am importing the  "net/http" library to be able to do http requests. Inside the main function, I call the `HandleFunc()` method of the `http` object. This method takes two parameters, the path and a callback function that gets executed when you append that path to the url. The callback has two params that allow us to work with the request and response objects that come in and go out of the server. In the case above, we call `Write()` on `w` and pass to it the data that we want to send back to the browser.
 
There is also a `ListenAndServe`  method that gets called to specify the port number. Here our server is listening on port `8000`. 

 If you are used to running node servers, you would find running the server a little bit strange. First, you have to install the app with  the following command

```go install myapp```

Immediately after a `bin` folder is created at the root directory containing the `src` folder, where your app `myapp` resides.
If you are on mac, the next command will immediately start the server for you:

```./bin/myapp```

Now simply go to your browser and type in https://localhost:8000 in the address bar and you should see the message `Hey!` on the browser.

Follow me @codero
👍  , , , , , , ,
properties (23)
authorcodero
permlinkmy-first-experince-running-a-go-web-server
categoryprogramming
json_metadata{"tags":["programming","software","education","computers","steem"],"image":["https://steemitimages.com/DQmdkSjpHyyeXwNQNWbSaCV4AcPJfveEgjCLEnFrzfQEf7H/golang.png"],"links":["https://localhost:8000"],"app":"steemit/0.1","format":"markdown","users":["codero"]}
created2017-09-09 12:30:36
last_update2017-09-09 14:31:15
depth0
children2
last_payout2017-09-16 12:30:36
cashout_time1969-12-31 23:59:59
total_payout_value0.037 HBD
curator_payout_value0.002 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,256
author_reputation15,333,700,816
root_title"My first experience running a Go web server"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,352,347
net_rshares14,368,359,874
author_curate_reward""
vote details (8)
@minnowpond ·
This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond
properties (22)
authorminnowpond
permlinkre-my-first-experince-running-a-go-web-server-20170909t124210
categoryprogramming
json_metadata"{"app": "pysteem/0.5.4"}"
created2017-09-09 12:42:12
last_update2017-09-09 12:42:12
depth1
children0
last_payout2017-09-16 12:42:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length143
author_reputation13,239,048,956,578
root_title"My first experience running a Go web server"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,353,152
net_rshares0
@phil-coding ·
This is really interesting information thank for that. I really thinking about start working with GO.
👍  ,
properties (23)
authorphil-coding
permlinkre-codero-my-first-experince-running-a-go-web-server-20170909t135937914z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-09 14:01:15
last_update2017-09-09 14:01:15
depth1
children0
last_payout2017-09-16 14:01:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length101
author_reputation4,773,758,818
root_title"My first experience running a Go web server"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,359,211
net_rshares2,266,167,394
author_curate_reward""
vote details (2)