Skip to main content

29 posts tagged with "golang"

View All Tags

· One min read
_sql := "select tid, bid, cover, title,limit_content,create_time,see,comment from blog where draft = '0' and title like CONCAT('%',?,'%') order by id desc limit ?,? "
// to do
rows, err := stmt.Query(title, (page-1)*FRONT_SIZE, FRONT_SIZE)

limit A, B: A是起始位置,等于(pageNum-1)*SIZE, B为SIZE(相当于步长)

实现前端分页API需要返回2个数据:

  • 总页数
  • 前端给定页数n,返回第n页的数据

· One min read
package main

import (
"github.com/scott-x/email"
"github.com/scott-x/randomCode"
)

func main() {
subject := "Slimx News"
body := `【温馨提示】:有人正在尝试访问你的博客, 验证码为: `
code, _ := randomCode.Random(6)
body += code
//send email
email.SendEmail(subject, body)
}

· One min read

前言

有这样的需求,我们把excel的数据存库了,可以直接调用,但是不排除中间修改数据的情况,如果excel被修改数据就不是最新的,所以,在拿到数据之前需要做时间上的对比

coding

package main

import (
"log"
"os"
"time"
)

func main() {
log.Println(GetFileModTime("/Users/apple/desktop/a.txt"))
}

//获取文件修改时间 返回unix时间戳
func GetFileModTime(path string) int64 {
f, err := os.Open(path)
if err != nil {
log.Println("open file error")
return time.Now().Unix()
}
defer f.Close()

fi, err := f.Stat()
if err != nil {
log.Println("stat fileinfo error")
return time.Now().Unix()
}

return fi.ModTime().Unix()
}

· One min read

code

package main

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

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
})
log.Println("server is running at http://127.0.0.1:8888")
http.ListenAndServe(":8888", nil)
}

ipv6如何访问

· One min read

获取程序的pid很有用

拿到pid之后,你就可以使用操作系统自带的命令来查看更多关于这个pid的信息, eg

ps -p pid -v
package main

import (
"fmt"
"os"
"os/exec"
"strconv"
)

func main() {

pid := os.Getpid()
fmt.Printf("Process PID: %d \n", pid)

prc := exec.Command("ps", "-p", strconv.Itoa(pid), "-v")
out, err := prc.Output()
if err != nil {
panic(err)
}

fmt.Println(string(out))

}

· 2 min read

信号:操作系统和程序之间初级交流方式

它来自古老的c语言,可以通过man signal查看详情

信号列表:

常用的信号有:

  • SIGINTSIGTERM 都是用来终端程序的
  • SIGUP: 程序后台挂起

Golang也可以处理信号, 示例如下:

package main

import (
"fmt"
"os"
"os/signal"
"syscall"
)

func main() {

// Create the channel where the received
// signal would be sent. The Notify
// will not block when the signal
// is sent and the channel is not ready.
// So it is better to
// create buffered channel.
sChan := make(chan os.Signal, 1)

// Notify will catch the
// given signals and send
// the os.Signal value
// through the sChan
signal.Notify(sChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGKILL)

// Create channel to wait till the
// signal is handled.
exitChan := make(chan int)
go func() {
signal := <-sChan
switch signal {
case syscall.SIGHUP:
fmt.Println("The calling terminal has been closed")
exitChan <- 0

case syscall.SIGINT:
fmt.Println("The process has been interrupted by CTRL+C")
exitChan <- 1

case syscall.SIGTERM:
fmt.Println("kill SIGTERM was executed for process")
exitChan <- 1

case syscall.SIGKILL:
fmt.Println("SIGKILL handler")
exitChan <- 1

case syscall.SIGQUIT:
fmt.Println("kill SIGQUIT was executed for process")
exitChan <- 1
}
}()

code := <-exitChan //here blocked
os.Exit(code)
}

运行代码,可以看到程序是被block住的:

通过按CTRL + C 发送SIGINT信号给程序, 程序退出