Wednesday, January 22, 2020

Membuat website sederhana menggunakan Golang

Di postingan kali ini kita akan membuat website sederhana menggunakan golang. So pertama-tama silahkan temen-temen dowload compiler golang yang terbaru dari https://golang.org/dl/

Setelah didownload silahkan di install, tinggal next next sahaja...
Nah setelah itu kita bisa langsung membuat source code website sederhananya seperti ini :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import(
    "fmt"
    "log"
    "net/http"
)

func baseHandler(w http.ResponseWriter, r *http.Request) {
 fmt.Fprint(w, "<h1> Hellow Maryadi </h1>")
}

func main() {
 http.HandleFunc("/", baseHandler)
 log.Fatal(http.ListenAndServe(":1111", nil))
}

So disini kita buat package main dan fungsi main (line 13). Fungsi main adalah fungsi utama yang akan dieksekusi ketika program berjalan seperti dalam C, C++, Java dsb yang masing-masing punya fungsi main juga.
Kemudian dibaris ke-14 ada fungsi http.HandleFunc. Fungsi ini berguna untuk mendeteksi path apa yang diakses oleh user. Kalau kita lihat input pertamanya adalah "/" berarti ketika kita akses path "/" diwebsite ini tar fungsi baseHandler akan dieksekusi.
Fungsi baseHandler sendiri bisa dilihat dibaris 9. Input fungsinya ada 2 yaitu w sebagai response ke user dan r (saat ini belum dipakai) sebagai variabel yang berasal dari user yang akses website kita. Dan bisa kita lihat response (w) hanya berisi html biasa yaitu <h1> Hellow Maryadi </h1>.
Terakhir dibaris 15 ada fungsi log buat memperlihatkan error message ke console kita. Daan http.ListenAndServe untuk menjalankan server. :1111 artinya server kita dapat diakses dari localhost port 1111. nil artinya null, artinya handler default dari server kita berasal dari http.HandleFunc.

Kalau kita ingin menjalankan servernya cukup dengan :
go run nama_file.go

Hasilnya :

Siiip... seperti itu saja... Terima kasih..semoga bermanfaat :D






Friday, December 6, 2019

Some motivations for our age

At age 23, Oprah was fired from her first reporting job. At age 24, Stephen King was working as a janitor and living in a trailer. At age 27, Vincent Van Gogh failed as a missionary and decided to go to art school. At age 28, J.K. Rowling was a suicidal single parent living on welfare. At age 30, Harrison Ford was a carpenter. At age 30, Martha Stewart was a stockbroker. Vera Wang failed to make the Olympic figure skating team, didn’t get the Editor-in-Chief position at Vogue, and designed her first dress at age 40. Stan Lee didn’t release his first big comic book until he was 40. Samuel L. Jackson didn’t get his first movie role until he was 46. Morgan Freeman landed his first movie role at age 52. Grandma Moses didn’t begin her painting career until age 76. Louise Bourgeois didn’t become a famous artist until she was 78. Whatever your dream is, it is not too late to achieve it. You aren’t a failure because you haven’t found fame and fortune by the age of 21. Hell, it’s ok if you don’t even know what your dream is yet. Never tell yourself you’re too old to make it. Never tell yourself you missed your chance. Never tell yourself that you aren’t good enough. You can do it. Whatever it is.

Source: