公告版位
需要找什麼嗎? 左邊分類或許可以幫助你...

目前分類:[程式語言] Go (31)

瀏覽方式: 標題列表 簡短摘要

package main

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

func indexHandle(w http.ResponseWriter, r *http.Request) {

    d := "<html><table border=1>";

黃彥霖 發表在 痞客邦 留言(0) 人氣()

以下為 HTML程式碼,請新建 index.html 檔案,複製下面程式碼後,再把它放在專案目錄下

<html>
    <body>
        Hello {{.Name}} !!
    </body>
</html>

---------------------------------------------------------------------
以下為 Go 語言 程式碼:

黃彥霖 發表在 痞客邦 留言(0) 人氣()

匯入 "time" 包,然後將以下程式碼加到你的程式碼中,就會有延遲效果 

time.Sleep(1 * time.Second)



黃彥霖 發表在 痞客邦 留言(1) 人氣()

os.Mkdir("A", os.ModePerm); // 當前目錄建立 A 資料夾
os.MkdirAll("A/B/C", os.ModePerm); // 當前目錄建立 多層 資料夾

os.Remove("A.txt") // 刪除檔案 (可多層)
os.RemoveAll("A") // 刪除資料料夾 (可多層)

// 以下為判斷該目錄或檔案是否存在,並判斷是資料夾還是檔案

finfo, err := os.Stat("AB")

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
    "net/http"
    "fmt"
)

func loadin(w http.ResponseWriter, r *http.Request) {
    username := r.FormValue("username")
    password := r.FormValue("password")
    fmt.Fprintf(w,"<html><body>");
    fmt.Fprintf(w, "Hello, %s ! <br/>", username)

黃彥霖 發表在 痞客邦 留言(0) 人氣()


先建立一個 index.html 檔,把下面 HTML 語法複製貼上,再將 html 檔移動到 Golang 專案下

<html>
<head>
<title>Golang upload</title>
</head>
<body>
<form id="uploadForm" method="POST" enctype="multipart/form-data" action="http://127.0.0.1:8080/upload">
<p>Golang upload</p>
<input type="FILE" id="file" name="file" />

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
    "fmt"
    "net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>Hello %s!</h1><br><br><image src=/img/A.jpg />", r.URL.Path[1:])
}

func img(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, r.URL.Path[1:])

黃彥霖 發表在 痞客邦 留言(0) 人氣()

// 目的資料夾一定要存在,否則不會自動幫你建立
// 如果原本已經有檔案在的話,會覆蓋掉


package main

import (
    "bufio"
    "os"
)


黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
        "bufio"
        "net"
)

func main() {

        listener, _ := net.Listen("tcp", ":80")
        println("啟動伺服器...")

        for {
                conn, _ := listener.Accept() // 持續監聽客戶端連線
                go ClientLogic(conn)
        }
}

func ClientLogic(conn net.Conn) {

        // 從客戶端接收資料
        s, _ := bufio.NewReader(conn).ReadString('\n')
        println("由客戶端傳來的訊息:", s)

        // 發送 資料至客戶端
        conn.Write([]byte("安安你好\n"))

        // 關閉連線
        conn.Close()
}
 





黃彥霖 發表在 痞客邦 留言(0) 人氣()

//---------------------------------- 簡單版
package main

import (
    "fmt"
    "encoding/gob"
    "os"
)

func main() {
    CreateFile()
    ReadFile()

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
    "os"
)

func main() {

    f, _ := os.Create("A.txt") // 建立 A.txt 文字檔
    f.WriteString("ABCD"); // 寫入 ABCD 文字

}

黃彥霖 發表在 痞客邦 留言(0) 人氣()

想要在 Eclipse 或其他 IDE 中使用方便的 GoCode ,GoCode 集成所有涵式庫,使寫程式有方便的輔助下拉式選單可以選擇。

安裝指令:

go get github.com/nsf/gocode

go install github.com/nsf/gocode



687474703a2f2f6e6f736d696c65666163652e72752f696d616765732f676f636f64652d73637265656e73686f742e706e67

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
    "net"
    "os"
)

const (
    RECV_BUF_LEN = 1024 // 接收的緩存大小
)

func main() {
    println("啟動伺服器...")

黃彥霖 發表在 痞客邦 留言(0) 人氣()

宣告:

    // 1024代表Byte的大小

    s := make([]byte, 1024

    var s [] byte = make([] byte, 1024)


轉型:

    [] Byte 轉 String :string (byte[])

黃彥霖 發表在 痞客邦 留言(0) 人氣()

    建立一條 Goroutine 後,必須要延遲堵塞通道(沒有值傳入)才能讓程式有時間去執行 Goroutine 的涵式,否則一呼叫 Goroutine 涵式,結果程式就結束了。以下程式碼是最好的例子:

 

package main

import (
     "fmt"
)

func main(){
    go A() // 產生 Goroutine
    // 程式結束

}

黃彥霖 發表在 痞客邦 留言(0) 人氣()

宣告一個 Map 的規則是:m := map[<Key鑰匙型態>]<Value型態>{}
如以下範例:

    m := map [intint {}
    m := map [stringint {}
    m := map [intstring {}
    m := map [int] *int {}
    ....等等


但是我們也可以在宣告的時候就順便給值:m := map[string]int{<key值>:<value值>, <key值>:<value值>}

黃彥霖 發表在 痞客邦 留言(0) 人氣()

        請先在命令提示字元中安裝 Go 的 PostgreSQL 驅動。

最新資訊可以參考:https://github.com/lib/pq
命令提示字元中輸入:go get github.com/lib/pq 
接著再輸入:go install github.com/lib/pq
如果沒有出現任何錯誤訊息的話,恭喜!! 安裝驅動完畢 !!

        我先在 PostgerSQL 建立一個 test 資料庫,使用者名稱是 postgres,密碼是 a 
接著,我在 test 資料庫中新增一個名稱為 my 的資料表,裡面分別有 use 與 pw 欄位。

他們看起來像這樣:

黃彥霖 發表在 痞客邦 留言(0) 人氣()

假設有一個 hello.go 檔,他必須放在 <你的目錄>/src/a/hello.go 上,src 一定要是這個名稱,Go 編譯器會找不到。接著 hello.go 底下的程式碼第一行醫定要寫 package a,因為你這個檔案是放在 a 資料夾底下。

接著開啟命令提示字元,將目錄切換到 <你的目錄>/src/ 路徑,接著輸入:go install a ,它會自動幫你編譯,並且安裝,接著我們主程式只要 import a 就可以呼叫。

 

 

 

 


黃彥霖 發表在 痞客邦 留言(0) 人氣()

package main

import (
        "fmt"
)

func main() {
       x := 3                          // 宣告 x 等於 3
       y := &x                        // 宣告 y 指向 x
       fmt.Println(*y, x)         // 輸出 : 3 3
       *y = 5                         // 指針 y 等於 5
       fmt.Println(*y, x)         // 輸出 : 5 5

黃彥霖 發表在 痞客邦 留言(0) 人氣()

簡單版本:

package main

import (
       "fmt"
       "net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
       fmt.Fprintf(w, "<h1>Hello !</h1>")
}


黃彥霖 發表在 痞客邦 留言(1) 人氣()

1 2