VARIS VONGUEA-AREE

🔥🇹🇭 Bangkok Thailand

High level experience in web design and development knowledge, producing quality work.

For more info
  • Home
  • About
  • Product
  • Blog
    • th
  • Varisz
  • Golang
  • Go (Golang) เบื้องต้น

Go (Golang) เบื้องต้น

มาจาก google

เป็นภาษาที่ compiler ได้เร็วมาก รองรับมัลติคอ ใน Golang ไม่มี OOP แบบ Class แต่มี Struct และ Interface

Golang จะเน้นหนักไปที่ gRPC https://www.blognone.com/node/84825

ในการเขียน golang นั้น เราจะไม่สนใจชื่อไฟล์ เราจะสนใจชื่อ package

จากหนังสือ Learning Go Programming
go env          // check path
go run index.go //Run Golang
go run test/*.go //Run every file .go 
go build        //Executable file
go install      //Install Ex. go get github.com/graphql-go/graphql
go get ชื่อแพ็คเกจ  //Install Package
go version       //check version golang

Example เบื้องต้น https://gowebexamples.com/

import(
"github.com/vektah/gqlgen/handler"   //import แบบปกติ
_ "github.com/vektah/gqlgen/handler"  //import โดยไม่ต้องเรียกใช้ก็ได้ 
. "github.com/vektah/gqlgen/handler"  //import เฉพาะสิ่งที่ export
)

Golang จะเริ่มด้วยการ รันไฟล์ฟังก์ชั่น func main() ก่อนเสมอ

Install

sudo snap install go --classic

GOROOT

go env GOROOT

GOPATH

go env GOPATH
C:\Users\%USERPROFILE%\go

Go Module

เมื่อเริ่มสร้างโปรเจ็คใหม่ต้องทำการสร้าง Go Module ก่อน เพื่อเตรียมไฟล์ที่ใช้ในโปรเจ็คนั้น เหมือน package.json ของ Nodejs

go mod init <project_name>
go test
หลังจากนั้นจะได้ไฟล์ go.mod และ go.sum
go mod download     //สำหรับโหลด pkg ในโปรเจ็คนั้น
go generate  //ติดตั้ง go dependencies 

Go Test

Data Type

uint8       unsigned  8-bit integers (0 to 255)
uint16      unsigned 16-bit integers (0 to 65535)
uint32      unsigned 32-bit integers (0 to 4294967295)
uint64      unsigned 64-bit integers (0 to 18446744073709551615)
int8        signed  8-bit integers (-128 to 127)
int16       signed 16-bit integers (-32768 to 32767)
int32       signed 32-bit integers (-2147483648 to 2147483647)
int64       signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     IEEE-754 32-bit floating-point numbers
float64     IEEE-754 64-bit floating-point numbers
complex64   complex numbers with float32 real and imaginary parts
complex128  complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

https://www.digitalocean.com/community/tutorials/understanding-data-types-in-go

Variable

var game string            // ประกาศตัวแปรประเภทตัวอักษร
nickName :=  "testt txt"   // := คือการประกาศตัวแปร
_ = nickName               // นำตัวแปรมาใช้ ทำให้ go ไม่มองว่าไม่จำเป็นvar game[5] int
game[0] = 10
a :=[]int{1,2,3,4,5,6,7,8,9,10}

var isTrue bool            //boolean

Collections

Array

a := [3]int{1, 2, 3}
b := [2]string{"Hello", "World"}
c := []int{1,2,3,4,5}

d := c[:] //slice element
e := c[2:] //slice element 

fmt.Println(a)

Structs

มีประเภท int string time.Time

type person struct {
   Name     string
   NickName string
}
-----------------
p1 := person{
Name:     "Test",
NickName: "Hero",
}
fmt.Println(p1)-------------------
var p1 person
p1.Name = "Test"
p1.NickName = "Hero"
fmt.Println(p1)

Map

map[string]string{"name": "Por", "sex": "male"}

Condition

if-else

if game == ""{ return } 

swich

i := 10
switch i{
 case i=5:
 case i=10:
 default:
}

Loop

For

var a [5]int
a[0] = 10
a[2] = 30
a[3] = 49

for i := range a {
   fmt.Println(a[i])
}

for _,v := range a {
   fmt.Println(v)
}

Controlflow

defer

ทำงานหลังจาก main function เสร็จแล้ว

panic

panic("Error")

Pointers

Function

c, b := 1, 2
r := add(c, b)
fmt.Println(r)

func init(){
 //เป็นฟังก์ชั่นพิเศษจะถูกเรียก ทำงานก่อนฟังก์ชั่น main()
}

func add(x, y int) int {    //ต้องระบุชนิดข้อมูลที่คืนค่ากลับไปด้วย ในนี้ int
  p := 1
  return x + y + p
}

Interface

Goroutines

Channels

Package

fmt

fmt.Println("") //แสดงผลข้อมูล
fmt.Println(a)  //แสดงผลข้อมูล
fmt.Printf("Hello, %s \n")  //แสดงข้อมูลแบบมีตัวแปร string
fmt.Scanln(&fruit)  //เก็บค่าใน pointerhttp.ServeFile(w, r, "index.html")   //เรียกไฟลฺ html มาแสดง

HTTP Server

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Welcome to my website!")
	})

	fs := http.FileServer(http.Dir("static/"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

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

Related

Vagrant
Fly IO
WordPress Woocommerce
DB Diagram

© 2021 All rights reserved by varisz