第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

咕嚕咕嚕:更新文件服務器提供的文件

咕嚕咕嚕:更新文件服務器提供的文件

Go
回首憶惘然 2022-10-04 16:02:10
我正在使用文件服務器為目錄提供服務,如下所示:go func() {  fs := http.FileServer(http.Dir("./view"))  err := http.ListenAndServe(":8000", fs)  if err != nil {    log.Fatal("ListenAndServe: ", err)  }}()在目錄中,我有一個文件,我正在嘗試在提供目錄時更新該文件。我觀察到追加命令僅在我停止提供目錄后阻止并更新文件。viewindex.htmlview以下是修改該文件的代碼:func AppendToFile() {  f, err := os.OpenFile("./view/index.html", os.O_RDWR, 0644)  if err != nil {    panic(err)  }  defer f.Close()  // This assumes that the file ends with </body></html>  f.Seek(-15, 2)  if _, err = f.WriteString("test test test\n"); err != nil {    panic(err)  }  if _, err = f.WriteString("</body></html>\n"); err != nil {    panic(err)  }}這是預期的行為嗎?謝謝!
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

網(wǎng)易娛樂.文件服務器函數(shù)只返回一個處理程序。因此,它不會阻塞文件系統(tǒng)。此處的問題可能與文件的偏移量有關(guān)。我已經(jīng)在我的機器中嘗試過,它工作沒有任何問題。


我已經(jīng)修改了你的代碼;


package main


import (

    "net/http"

    "os"

    "time"

)


func main() {

    t := time.NewTicker(time.Second)

    defer t.Stop()

    go func() {

        srv := http.FileServer(http.Dir("./test"))

        http.ListenAndServe(":8080", srv)

    }()


    for {

        select {

        case <-t.C:

            appendToFile()

        }

    }

}


func appendToFile() {

    f, err := os.OpenFile("./test/index.html", os.O_RDWR, 0644)

    if err != nil {

        panic(err)

    }

    defer f.Close()

    // This assumes that the file ends with </body></html>

    f.Seek(-16, 2)

    if _, err = f.WriteString("test test test\n"); err != nil {

        panic(err)

    }

    if _, err = f.WriteString("</body></html>\n"); err != nil {

        panic(err)

    }

}

在索引中.html我最初放置空白文檔,


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>


</body></html>

PS:最好先檢查偏移量,然后將字符串寫入該位置。


查看完整回答
反對 回復 2022-10-04
  • 1 回答
  • 0 關(guān)注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號