beego瀛︿範绗旇涓€锛氬垱寤虹涓€涓猙eego Web椤圭洰 杞

旧时模样 提交于 2020-04-28 07:31:51

 

鍓嶆彁宸ヤ綔

鐜鎼缓锛屽彲浠ュ弬鑰冨涓嬩袱绡囨暀绋嬶細
鎼缓Go璇█鐜1
鎼缓Go璇█鐜2

瀹夎beego

beego 鐨勫畨瑁呮槸鍏稿瀷鐨 Go 瀹夎鍖呯殑褰㈠紡锛欬/p>

go get github.com/astaxie/beego

甯歌闂锛欬/p>

  • git https 鏃犳硶鑾峰彇锛岃閰嶇疆鏈湴鐨 git锛屽叧闂 https 楠岃瘉锛欬/li>
git config --global http.sslVerify false
  • 濡傚洜涓虹綉缁滈棶棰樻棤娉曚笅杞斤紝璇峰弬鑰冧笅闈㈢殑婧愮爜涓嬭浇鍗囩骇鏂瑰紡

beego 鍗囩骇鍒嗕负 go 鏂瑰紡鍗囩骇鍜屾簮鐮佷笅杞藉崌绾э細

  • Go 鍗囩骇锛岄€氳繃璇ユ柟寮忕敤鎴峰彲浠ュ崌绾 beego 妗嗘灦锛屽己鐑堟帹鑽愯鏂瑰紡锛欬/li>
go get -u github.com/astaxie/beego
go install  github.com/astaxie/beego

瀹夎bee宸ュ叿

bee 宸ュ叿鏄竴涓负浜嗗崗鍔╁揩閫熷紑鍙 beego 椤圭洰鑰屽垱寤虹殑椤圭洰锛岄€氳繃 bee 鎮ㄥ彲浠ュ緢瀹规槗鐨勮繘琛 beego 椤圭洰鐨勫垱寤恒€佺儹缂栬瘧銆佸紑鍙戙€佹祴璇曘€佸拰閮ㄧ讲銆侟/p>

閫氳繃濡備笅鐨勬柟寮忓畨瑁 bee 宸ュ叿锛欬/p>

go get github.com/beego/bee

瀹夎瀹屼箣鍚庯紝bee鍙墽琛屾枃浠堕粯璁ゅ瓨鏀惧湪 $GOPATH/bin閲岄潰锛屾墍浠ラ渶瑕佹妸$GOPATH/bin娣诲姞鍒版偍鐨勭幆澧冨彉閲忎腑锛屾墠鍙互杩涜涓嬩竴姝ャ€侟br>鍦ㄥ懡浠よ杈撳叆bee,濡傛灉鍑虹幇濡備笅淇℃伅鍒欒〃绀哄畨瑁呮垚鍔烖/p>

 

 
 

鎴戜滑鍙互鍦?code>GOPATH/bin鐩綍涓嬫壘鍒扮敓鎴愮殑bee.exe鍙墽琛屾枃浠讹紝濡備笅锛欬/p>

 

 

 

鏂板缓beego web椤圭洰

new鍛戒护鏄柊寤轰竴涓 Web 椤圭洰锛屽湪鍛戒护杈撳叆bee new <椤圭洰鍚?gt;锛屾瘮濡傛垜浠緭鍏ュ懡浠|code>bee new myapp锛岃繍琛岀粨鏋滃涓嬶細

 

 
 

bee宸ュ叿浼氳嚜鍔ㄥ湪$GOPATH/src鐩綍涓嬬敓鎴恗yapp椤圭洰锛屽涓嬶細

 

鍚姩椤圭洰

鍛戒护绐楀彛瀹氫綅鍒癿yapp鐩綍涓嬶紝杩愯鍛戒护bee run鍗冲彲鍚姩椤圭洰

 

 

 

鍚姩鎴愬姛锛岀孩妗嗛儴鍒嗘樉绀洪」鐩繍琛岀鍙d负8080锛屾垜浠€氳繃娴忚鍣ㄨ闂細localhost:8080锛屽嵆鍙繘鍏ュ涓嬬晫闈↑/p>

 

 

鍐欎竴涓猦ello world

鐢╥dea鎵撳紑璇ラ」鐩紝涓嬮潰鏄暣浣撶殑椤圭洰缁撴瀯

 

鍦╟ontrollers鐩綍涓嬫柊寤篽ello.go鏂囦欢锛屽唴瀹瑰涓嬶細

package controllers

import (
    "github.com/astaxie/beego" ) type HelloController struct { beego.Controller //杩欓噷鐩稿綋浜庣户鎵縝eego.Controller } //閲嶅啓Get鏂规硶 func (hello *HelloController) Get() { hello.Ctx.WriteString("hello go") } 

涓婇潰鐨勪唬鐮佹樉绀洪鍏堟垜浠0鏄庝簡涓€涓帶鍒跺櫒 HelloController锛岃繖涓帶鍒跺櫒閲岄潰鍐呭祵浜咟code>beego.Controller锛岃繖灏辨槸 Go 鐨勫祵鍏ユ柟寮忥紝涔熷氨鏄?code>HelloController鑷姩鎷ユ湁浜嗘墍鏈 beego.Controller 鐨勬柟娉曘€傝繖绫讳技浜庨潰鍚戝璞¢噷鐨勭户鎵裤€侟br>鑰孅code>beego.Controller鎷ユ湁寰堝鏂规硶锛屽叾涓寘鎷?code>Init銆丳repare銆丳ost銆丟et銆丏elete銆丠ead绛夋柟娉曘€傛垜浠彲浠ラ€氳繃閲嶅啓鐨勬柟寮忔潵瀹炵幇杩欎簺鏂规硶锛岃€屾垜浠笂闈㈢殑浠g爜灏辨槸閲嶅啓浜咟code>Get鏂规硶銆侟/p>

鐒跺悗灏嗘柊澧炵殑HelloController娣诲姞璺敱锛屼慨鏀箁outers/router.go鏂囦欢锛屽涓嬶細

package routers

import (
    "myapp/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) // 鏂板璺敱 beego.Router("/hello", &controllers.HelloController{}) } 

濂戒簡锛屾帴涓嬫潵鏄笉鏄兂鐫€瑕侀噸鍚」鐩紵涓嶇敤锛宐ee宸ュ叿榛樿涓烘垜浠殑beego椤圭洰瀹炵幇浜嗙儹鍔犺浇锛屾垜浠湪鏉ョ湅鐪嬫帶鍒跺彴

 

 

鏀瑰姩浠g爜鏃犻渶閲嶅惎椤圭洰锛岀畝鐩翠笉瑕佸お鐖斤紒

璁╁悗鎴戜滑鎵撳紑娴忚鍣ㄨ緭鍏?a href="http://localhost:8080/hello" target="_blank" rel="nofollow">http://localhost:8080/hello 鐪嬬粨鏋淈/p>

杞嚜  https://www.jianshu.com/p/bdf5bc7e1c6c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!