最近發(fā)現(xiàn)了一款非常好用的基于go語言的web開發(fā)框架,非常適合PHP轉(zhuǎn)Go的同學(xué)使用,在很多設(shè)計(jì)思想和使用上和PHP的Laravel框架非常像。
今天就為大家簡單介紹一下GoFrame的特點(diǎn):
官方介紹
GoFrame是一款模塊化、高性能、企業(yè)級的Go基礎(chǔ)開發(fā)框架。GoFrame不是一款WEB/RPC框架,而是一款通用性的基礎(chǔ)開發(fā)框架,是Golang標(biāo)準(zhǔn)庫的一個增強(qiáng)擴(kuò)展級,包含通用核心的基礎(chǔ)開發(fā)組件,優(yōu)點(diǎn)是實(shí)戰(zhàn)化、模塊化、文檔全面、模塊豐富、易用性高、通用性強(qiáng)、面向團(tuán)隊(duì)。
我的使用體驗(yàn)
官方文檔詳細(xì)介紹了框架特點(diǎn),我就不贅述了。
下面我以一個使用者和學(xué)習(xí)者的角度分享一下我的學(xué)習(xí)體會。
設(shè)計(jì)思想
設(shè)計(jì)思想是GoFrame框架的靈魂,同時對于使用者來講,是不可或缺的內(nèi)功心法。GoFrame有其獨(dú)特的設(shè)計(jì)思想,理解了GoFrame的設(shè)計(jì)思想,您就理解了GoFrame的全部。
和PHP的Laravel一樣,goframe的設(shè)計(jì)思想非常值得我們學(xué)習(xí)和借鑒。
學(xué)習(xí)建議
有基礎(chǔ)的同學(xué)
有基礎(chǔ)的同學(xué),建議可以簡單熟悉一下框架設(shè)計(jì)、操作一下快速開始,然后就重點(diǎn)閱讀 核心組件[1]
尤其是數(shù)據(jù)庫ORM需要重點(diǎn)看一下,熟悉Laravel Eloquent的同學(xué)看起來應(yīng)該比較輕松,很多使用和習(xí)慣是比較像的。
下面我舉個實(shí)例讓大家體會一下,從一些細(xì)節(jié)設(shè)計(jì)上我們能明顯感覺到設(shè)計(jì)者對PHP轉(zhuǎn)go開發(fā)者的友好。
對象管理相關(guān):
Array也是切片的別名,猜測是為了迎合PHP轉(zhuǎn)go的使用習(xí)慣,PHP的array和golang的slice切片更像,因?yàn)镚o的數(shù)組是固定長度的。
type(
Var = gvar.Var //是一個通用的變量,類似泛型
Ctx = context.Context //context.Context的別名
)
//Map是對原生map的key value約定好了類型,起了別名
type(
Map = map[string]interface{}
MapAnyAny = map[interface{}]interface{} // MapAnyAny is alias of frequently-used map type map[interface{}]interface{}.
MapAnyStr = map[interface{}]string // MapAnyStr is alias of frequently-used map type map[interface{}]string.
MapAnyInt = map[interface{}]int // MapAnyInt is alias of frequently-used map type map[interface{}]int.
MapStrAny = map[string]interface{} // MapStrAny is alias of frequently-used map type map[string]interface{}.
MapStrStr = map[string]string // MapStrStr is alias of frequently-used map type map[string]string.
MapStrInt = map[string]int // MapStrInt is alias of frequently-used map type map[string]int.
MapIntAny = map[int]interface{} // MapIntAny is alias of frequently-used map type map[int]interface{}.
.
.
.
)
//List是map類型的切片
type (
List = []Map
ListAnyAny = []MapAnyAny // ListAnyAny is alias of frequently-used slice type []MapAnyAny.
ListAnyStr = []MapAnyStr // ListAnyStr is alias of frequently-used slice type []MapAnyStr.
ListAnyInt = []MapAnyInt // ListAnyInt is alias of frequently-used slice type []MapAnyInt.
ListStrAny = []MapStrAny // ListStrAny is alias of frequently-used slice type []MapStrAny.
ListStrStr = []MapStrStr // ListStrStr is alias of frequently-used slice type []MapStrStr.
ListStrInt = []MapStrInt // ListStrInt is alias of frequently-used
.
.
.
)
//Slice就是切片的別名
type(
Slice = []interface{} // Slice is alias of frequently-used slice type []interface{}.
SliceAny = []interface{} // SliceAny is alias of frequently-used slice type []interface{}.
SliceStr = []string // SliceStr is alias of frequently-used slice type []string.
SliceInt = []int // SliceInt is alias of frequently-used slice type []int.
)
//Array也是切片的別名,猜測是為了迎合PHP轉(zhuǎn)go的使用習(xí)慣,PHP的array和golang的切片更像,因?yàn)間o的數(shù)組的固定長度的。
type(
Array = []interface{} // Array is alias of frequently-used slice type []interface{}.
ArrayAny = []interface{} // ArrayAny is alias of frequently-used slice type []interface{}.
ArrayStr = []string // ArrayStr is alias of frequently-used slice type []string.
ArrayInt = []int // ArrayInt is alias of frequently-used slice type []int.
)
無基礎(chǔ)的同學(xué)
無Go語言基礎(chǔ)的同學(xué),我建議先學(xué)Go的基礎(chǔ)語法,可以訂閱一下我的GO語言學(xué)習(xí)專欄,好好學(xué)習(xí)一下Go基礎(chǔ),然后再看Goframe的框架。
因?yàn)橹挥懈闱宄礼o語言基礎(chǔ)后,才能更好理解GoFrame的優(yōu)勢和使用技巧。
就像我們做PHP的時候,一定是先學(xué)習(xí)PHP的基礎(chǔ)語法,然后才學(xué)TP、Laravel這類框架的。
對于有PHP基礎(chǔ),只是沒有Go語言基礎(chǔ)的同學(xué)來講,轉(zhuǎn)Go還是比較輕松的。
可能只是不能像PHP那么靈活,那么隨心所欲的寫代碼了,嘗試一下GO語言苛刻的規(guī)范化開發(fā)也未嘗不是一種享受。
官網(wǎng)地址
復(fù)制粘貼的重復(fù)工作我就不做了,更多內(nèi)容建議大家查看下方的官網(wǎng)。
目前最新的2.0版本[2]
小坑
在看文檔過程中,我們不能很明顯的知道當(dāng)前文檔的版本,這個問題我已經(jīng)提交給社區(qū)了,目前的閱讀建議是這樣,我們把頁面拉到最上面,點(diǎn)擊左上角這里進(jìn)行版本切換。

相關(guān)資料
[1]核心組件: https://goframe.org/pages/viewpage.action?pageId=1114409
[2]目前最新的2.0版本: https://goframe.org/pages/viewpage.action?pageId=1114119
本文轉(zhuǎn)載自微信公眾號「 程序員升級打怪之旅」,作者「王中陽Go」,可以通過以下二維碼關(guān)注。

轉(zhuǎn)載本文請聯(lián)系「 程序員升級打怪之旅」公眾號。