<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          go全棧開發(fā)框架bud,go語言中的ruby on rails

          共 3786字,需瀏覽 8分鐘

           ·

          2022-05-31 18:35

          • 介紹

          Bud是一個(gè)go語言編寫的全棧開發(fā)框架,類似ruby on railsBud的使命是降低構(gòu)建、開發(fā)和維護(hù)web應(yīng)用的成本。Bud提供了一整套文檔完善的、充分測試的工具來降低web應(yīng)用開發(fā)成本。

          • 安裝

          curl -sf https://raw.githubusercontent.com/livebud/bud/main/install.sh | shbud -h
          • 創(chuàng)建web app

          bud create hellocd hellolsgo.mod  node_modules/  package-lock.json  package.json

          不像其他全??蚣?,bud生成的初始代碼非常少,再添加依賴之后,bud才會(huì)生成所有的代碼。

          bud run運(yùn)行項(xiàng)目,開啟一個(gè)開發(fā)模式的Server:

          bud run| Listening on http://0.0.0.0:3000

          • 目錄結(jié)構(gòu)

          Bud提供了一個(gè)標(biāo)準(zhǔn)目錄結(jié)構(gòu):

          $YOUR_APP├─ bud├─ controller├─ internal├─ public└─ view
          • bud/ 這個(gè)目錄是創(chuàng)建的時(shí)候自動(dòng)生成的,包含框架生成的代碼,偶爾會(huì)需要從這個(gè)目錄導(dǎo)入packages,但大部分時(shí)候都可以忽略它,建議不要放在版本控制器里

          • controller/ 控制器目錄用來獲取http請求,并響應(yīng)返回

          • view/ 視圖目錄用來存放視圖文件

          • public/ public目錄用來存放靜態(tài)資源

          • internal/ 這個(gè)目錄不會(huì)被Bud框架引用,建議放這個(gè)應(yīng)用特定的packages

          • 控制器和路由

          Bud的路由不同于傳統(tǒng)的go框架,它遵循一套約定:每個(gè)控制器對應(yīng)一組標(biāo)準(zhǔn)路由,也可以自定義Action,Action和路由一一對應(yīng),下面看例子:

          bud new controller post

          這個(gè)命令會(huì)生成

          controller/post/controller.go 此時(shí)這個(gè)文件里只能包含以下方法:

          可以選擇實(shí)現(xiàn)任意方法,如果要新增一個(gè)自定義路由,則需要新增一個(gè)方法:

          func (c *Controller) About(ctx context.Context) (string, error) {    return "hello", nil}

          這個(gè)方法有一個(gè)默認(rèn)路由/about,首字母大寫的Action會(huì)自動(dòng)獲得對應(yīng)小寫的路由,首字母小寫的Action不會(huì)獲得路由。

          • 請求和響應(yīng)

          解析請求框架會(huì)自動(dòng)完成,可以在action參數(shù)里接收,也可以使用單獨(dú)的結(jié)構(gòu)體接收,例如:

          func (c *Controller) About(ctx context.Context, name string) (string, error) {    return fmt.Sprintf("hello %s", name), nil}
          type Input struct { Name string}
          func (c *Controller) About(ctx context.Context, in *Input) (string, error) { return fmt.Sprintf("hello %s", in.Name), nil}

          一個(gè)控制器如果沒有對應(yīng)的視圖,總是返回json,例如:

          func (c *Controller) Create() (id int, email string) {  return 10, "[email protected]"}
          type Output struct { ID int Email string}
          func (c *Controller) Create() Output { return Output{ ID: 10, Email: "[email protected]", }}

          上述這兩種形式都將返回:

          {  "id": 10,  "email": "[email protected]"}
          • 視圖

          視圖用來展示HTML頁面,bud默認(rèn)使用svelte渲染視圖,svelte是一個(gè)高性能前端js框架。

          $YOUR_APP└─ view   ├─ index.svelte     -> Root index page   ├─ posts   │  ├─ edit.svelte   -> Edit post page   │  ├─ index.svelte  -> Post index page   │  ├─ new.svelte    -> New post page   │  ├─ show.svelte   -> Show post page   │  └─ Post.svelte   -> Post component (not rendered)   └─ users      ├─ edit.svelte   -> Edit user page      ├─ index.svelte  -> User index page      └─ show.svelte   -> Show user page

          控制器中返回的值,如果有對應(yīng)視圖文件,則值會(huì)被傳遞到視圖中:

          <script>
            export let users = [] //來自控制器
          </script>

          <h1>Users</h1>

          <ul>
              {#each users as user}
              <li>{user.name} is {user.age} years old</li>
              {/each}
          </ul>
          • 其他

          Bud框架并沒有自己的ORM,用戶可自行選擇orm框架,通過依賴注入在控制器中使用。另外,還有全棧框架包含的特性,如錯(cuò)誤處理、插件系統(tǒng)、數(shù)據(jù)驗(yàn)證及安全、日志、隊(duì)列與定時(shí)任務(wù)、文件與存儲(chǔ)等都可以在官方文檔上找到。

          附文檔鏈接:https://github.com/livebud/bud


          END



          -猜你想看-

          謝孟軍:中國 Go 語言領(lǐng)軍人創(chuàng)業(yè)第五年

          可替代 Jenkins:Drone 進(jìn)一步簡化 CI/CD


          想要了解Go更多內(nèi)容,歡迎掃描下方?? 關(guān)注 公眾號(hào),回復(fù)關(guān)鍵詞 [實(shí)戰(zhàn)群]  ,就有機(jī)會(huì)進(jìn)群和我們進(jìn)行交流~

          分享、在看與點(diǎn)贊,至少我要擁有一個(gè)叭~

          瀏覽 73
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99热人人操 | 99视频精品全部免费看 | 91精品久久久久久久不卡 | 国产夫妻自拍在线观看 | 天堂资源在线6 |