顯示具有 GO 標籤的文章。 顯示所有文章
顯示具有 GO 標籤的文章。 顯示所有文章

2023年7月6日 星期四

cannot find package "github.com/mattn/go-sqlite3"

apt install -y git gcc 

export GO111MODULE=off

go get github.com/mattn/go-sqlite3

2023年2月16日 星期四

GOLAMG static link

 go build -ldflags "-linkmode external -extldflags '-static'" exp.go

2023年1月30日 星期一

Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work.

apt  install gcc
CGO_ENABLED=1 go install github.com/mattn/go-sqlite3

go get github.com/mattn/go-sqlite3 失效

錯誤訊息
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.

執行以下指令
go mod init modulename
go mod tidy

2022年2月22日 星期二

GO GO111MODULE 與 GOPATH

GO111MODULE=off,使用 GOPATH mode 模式

GO111MODULE=on,使用 Go modules 模式

2019年1月4日 星期五

GOLANG 出現 cannot refer to unexported name

原因:Package 導出函數必須為大寫,通常出現在自定 Package 函數名稱使用小寫

2017年3月27日 星期一

golang 換行字串寫法

golang 換行字串寫法

`line 1
line 2
line 3`

2016年11月22日 星期二

GOLANG function 回傳不特定類型值

func a(x int) interface{}{
  if x==1 {   return nil  }
  if x==2 {   return  2 }
  return "string"
}

2016年11月17日 星期四

GOLANG Swap

b,c := 2,3

b,c = c,b

GOLANG Template 輸出至字串

var doc bytes.Buffer

t.Execute(&doc, data)
str := doc.String()

2016年10月11日 星期二

列出所有網路遮罩長度 IP

package main

import (
 "fmt"
 "math"
 "net"
 "encoding/binary"
)

  func main() {
    var ii uint32 = 0
    for i:=1; i<33; i++ {
      ii += uint32(math.Pow(2, float64(32-i)));
      fmt.Print(i, " ", long2ip(ii), "  ", ii, "\n")
    }
  }

        func ip2long(ipstr string) uint32 {
          ip := net.ParseIP(ipstr)
          if ip == nil {  return 0   }

          return binary.BigEndian.Uint32(ip.To4())
        }

        func long2ip(ipLong uint32) string {
          ipByte := make([]byte, 4)
          binary.BigEndian.PutUint32(ipByte, ipLong)

          return net.IP(ipByte).String()
        }

GOLANG 程式減肥

go build -ldflags "-s -w" src.go

GOLANG 跨平台編譯

使用 GOOS , GOARCH 環境變數設定
GOOS 表示作業系統 windows, linux
GOARCH 表示架構 386, amd64

2016年9月30日 星期五

GO Web cgi server

package main

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

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
    handler := new(cgi.Handler)
    handler.Path = "/root/goweb/" + r.URL.Path
//    handler.Dir = "/root/goweb/"

    log.Println(handler.Path)
    log.Println(handler.Args)

    handler.ServeHTTP(w, r)
  })

  log.Fatal(http.ListenAndServe(":8080",nil))
}

GO Web cgi server (直接執行 go )

package main

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

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
    handler := new(cgi.Handler)
    handler.Path = "/usr/local/bin/go"
    script := "/root/goweb/" + r.URL.Path
    log.Println(handler.Path)
    handler.Dir = "/root/goweb/"
    args := []string{"run", script}
    handler.Args = append(handler.Args, args...)
    handler.Env = append(handler.Env, "GOPATH=/root/goweb")
    handler.Env = append(handler.Env, "GOROOT=/usr/local/go")
    log.Println(handler.Args)

    handler.ServeHTTP(w, r)
  })

  log.Fatal(http.ListenAndServe(":8080",nil))
}

GOLANG CGI範例

package main

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

func errorResponse(code int, msg string) {
    fmt.Printf("Status:%d %s\r\n", code, msg)
    fmt.Printf("Content-Type: text/plain\r\n")
    fmt.Printf("\r\n")
    fmt.Printf("%s\r\n", msg)
}

func main() {
    var req *http.Request
    var err error
    req, err = cgi.Request()
    if err != nil {
        errorResponse(500, "cannot get cgi request" + err.Error())
        return
    }

    fmt.Printf("Content-Type: text/html\r\n")
    fmt.Printf("\r\n")
    fmt.Printf("<pre>\n")
    fmt.Printf("req=%v\r\n", req)
}

2016年9月19日 星期一

GOLANG MySQL 查詢範例

安裝 mySQL 介面
go get github.com/go-sql-driver/mysql


程式範例
package main

import (
    _ "github.com/go-sql-driver/mysql"
    "database/sql"
    "fmt"
)

func main() {
    db, err := sql.Open("mysql", "user:password@tcp(192.168.1.1:3306)/dbname?charset=utf8&allowOldPasswords=1")
    checkErr(err)

    rows, err := db.Query("SELECT `load` FROM a limit 10")
    checkErr(err)

     for rows.Next() {
       var load int
       err = rows.Scan(&load)
       checkErr(err)
       fmt.Println(load)

    }

    db.Close()
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}