欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

go語(yǔ)言需要依賴注入嗎,golang 依賴注入

GO語(yǔ)言(二十七):管理依賴項(xiàng)(下)-

當(dāng)您對(duì)外部模塊的存儲(chǔ)庫(kù)進(jìn)行了 fork (例如修復(fù)模塊代碼中的問(wèn)題或添加功能)時(shí),您可以讓 Go 工具將您的 fork 用于模塊的源代碼。這對(duì)于測(cè)試您自己的代碼的更改很有用。

泗洪ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

為此,您可以使用go.mod 文件中的replace指令將外部模塊的原始模塊路徑替換為存儲(chǔ)庫(kù)中 fork 的路徑。這指示 Go 工具在編譯時(shí)使用替換路徑(fork 的位置),例如,同時(shí)允許您保留import 原始模塊路徑中的語(yǔ)句不變。

在以下 go.mod 文件示例中,當(dāng)前模塊需要外部模塊example.com/theirmodule。然后該replace指令將原始模塊路徑替換為example.com/myfork/theirmodule模塊自己的存儲(chǔ)庫(kù)的分支。

設(shè)置require/replace對(duì)時(shí),使用 Go 工具命令確保文件描述的需求保持一致。使用go list命令獲取當(dāng)前模塊正在使用的版本。然后使用go mod edit命令將需要的模塊替換為fork:

注意: 當(dāng)您使用該replace指令時(shí),Go 工具不會(huì)像添加依賴項(xiàng)中所述對(duì)外部模塊進(jìn)行身份驗(yàn)證。

您可以使用go get命令從其存儲(chǔ)庫(kù)中的特定提交為模塊添加未發(fā)布的代碼。

為此,您使用go get命令,用符號(hào)@指定您想要的代碼 。當(dāng)您使用go get時(shí),該命令將向您的 go.mod 文件添加一個(gè) 需要外部模塊的require指令,使用基于有關(guān)提交的詳細(xì)信息的偽版本號(hào)。

以下示例提供了一些說(shuō)明。這些基于源位于 git 存儲(chǔ)庫(kù)中的模塊。

當(dāng)您的代碼不再使用模塊中的任何包時(shí),您可以停止將該模塊作為依賴項(xiàng)進(jìn)行跟蹤。

要停止跟蹤所有未使用的模塊,請(qǐng)運(yùn)行g(shù)o mod tidy 命令。此命令還可能添加在模塊中構(gòu)建包所需的缺失依賴項(xiàng)。

要?jiǎng)h除特定依賴項(xiàng),請(qǐng)使用go get,指定模塊的模塊路徑并附加 @none,如下例所示:

go get命令還將降級(jí)或刪除依賴于已刪除模塊的其他依賴項(xiàng)。

當(dāng)您使用 Go 工具處理模塊時(shí),這些工具默認(rèn)從 proxy.golang.org(一個(gè)公共的 Google 運(yùn)行的模塊鏡像)或直接從模塊的存儲(chǔ)庫(kù)下載模塊。您可以指定 Go 工具應(yīng)該使用另一個(gè)代理服務(wù)器來(lái)下載和驗(yàn)證模塊。

如果您(或您的團(tuán)隊(duì))已經(jīng)設(shè)置或選擇了您想要使用的不同模塊代理服務(wù)器,您可能想要這樣做。例如,有些人設(shè)置了模塊代理服務(wù)器,以便更好地控制依賴項(xiàng)的使用方式。

要為 Go 工具指定另一個(gè)模塊代理服務(wù)器,請(qǐng)將GOPROXY 環(huán)境變量設(shè)置為一個(gè)或多個(gè)服務(wù)器的 URL。Go 工具將按照您指定的順序嘗試每個(gè) URL。默認(rèn)情況下,GOPROXY首先指定一個(gè)公共的 Google 運(yùn)行模塊代理,然后從模塊的存儲(chǔ)庫(kù)直接下載(在其模塊路徑中指定):

您可以將變量設(shè)置為其他模塊代理服務(wù)器的 URL,用逗號(hào)或管道分隔 URL。

Go 模塊經(jīng)常在公共互聯(lián)網(wǎng)上不可用的版本控制服務(wù)器和模塊代理上開發(fā)和分發(fā)。您可以設(shè)置 GOPRIVATE環(huán)境變量。您可以設(shè)置GOPRIVATE環(huán)境變量來(lái)配置go命令以從私有源下載和構(gòu)建模塊。然后 go 命令可以從私有源下載和構(gòu)建模塊。

GOPRIVATE或環(huán)境變量可以設(shè)置為匹配模塊前綴的全局模式列表,這些GONOPROXY前綴是私有的,不應(yīng)從任何代理請(qǐng)求。例如:

Go語(yǔ)言的優(yōu)勢(shì)有哪些

1. 部署簡(jiǎn)單

Go

編譯生成的是一個(gè)靜態(tài)可執(zhí)行文件,除了glibc外沒有其他外部依賴。這讓部署變得異常方便:目標(biāo)機(jī)器上只需要一個(gè)基礎(chǔ)的系統(tǒng)和必要的管理、監(jiān)控工具,完全不需要操心應(yīng)用所需的各種包、庫(kù)的依賴關(guān)系,大大減輕了維護(hù)的負(fù)擔(dān)。

2. 并發(fā)性好

Goroutine和channel使得編寫高并發(fā)的服務(wù)端軟件變得相當(dāng)容易,很多情況下完全不需要考慮鎖機(jī)制以及由此帶來(lái)的各種問(wèn)題。單個(gè)Go應(yīng)用也能有效的利用多個(gè)CPU核,并行執(zhí)行的性能好。

3. 良好的語(yǔ)言設(shè)計(jì)

從學(xué)術(shù)的角度講Go語(yǔ)言其實(shí)非常平庸,不支持許多高級(jí)的語(yǔ)言特性;但從工程的角度講,Go的設(shè)計(jì)是非常優(yōu)秀的:規(guī)范足夠簡(jiǎn)單靈活,有其他語(yǔ)言基礎(chǔ)的程序員都能迅速上手。更重要的是

Go 自帶完善的工具鏈,大大提高了團(tuán)隊(duì)協(xié)作的一致性。

4. 執(zhí)行性能好

雖然不如 C 和 Java,但相比于其他編程語(yǔ)言,其執(zhí)行性能還是很好的,適合編寫一些瓶頸業(yè)務(wù),內(nèi)存占用也非常省。

golang反射框架Fx

Fx是一個(gè)golang版本的依賴注入框架,它使得golang通過(guò)可重用、可組合的模塊化來(lái)構(gòu)建golang應(yīng)用程序變得非常容易,可直接在項(xiàng)目中添加以下內(nèi)容即可體驗(yàn)Fx效果。

Fx是通過(guò)使用依賴注入的方式替換了全局通過(guò)手動(dòng)方式來(lái)連接不同函數(shù)調(diào)用的復(fù)雜度,也不同于其他的依賴注入方式,F(xiàn)x能夠像普通golang函數(shù)去使用,而不需要通過(guò)使用struct標(biāo)簽或內(nèi)嵌特定類型。這樣使得Fx能夠在很多go的包中很好的使用。

接下來(lái)會(huì)提供一些Fx的簡(jiǎn)單demo,并說(shuō)明其中的一些定義。

1、一般步驟

大致的使用步驟就如下。下面會(huì)給出一些完整的demo

2、簡(jiǎn)單demo

將io.reader與具體實(shí)現(xiàn)類關(guān)聯(lián)起來(lái)

輸出:

3、使用struct參數(shù)

前面的使用方式一旦需要進(jìn)行注入的類型過(guò)多,可以通過(guò)struct參數(shù)方式來(lái)解決

輸出

如果通過(guò)Provide提供構(gòu)造函數(shù)是生成相同類型會(huì)有什么問(wèn)題?換句話也就是相同類型擁有多個(gè)值呢?

下面兩種方式就是來(lái)解決這樣的問(wèn)題。

4、使用struct參數(shù)+Name標(biāo)簽

在Fx未使用Name或Group標(biāo)簽時(shí)不允許存在多個(gè)相同類型的構(gòu)造函數(shù),一旦存在會(huì)觸發(fā)panic。

輸出

上面通過(guò)Name標(biāo)簽即可完成在Fx容器注入相同類型

5、使用struct參數(shù)+Group標(biāo)簽

使用group標(biāo)簽同樣也能完成上面的功能

輸出

基本上Fx簡(jiǎn)單應(yīng)用在上面的例子也做了簡(jiǎn)單講解

1、Annotated(位于annotated.go文件) 主要用于采用annotated的方式,提供Provide注入類型

源碼中Name和Group兩個(gè)字段與前面提到的Name標(biāo)簽和Group標(biāo)簽是一樣的,只能選其一使用

2、App(位于app.go文件) 提供注入對(duì)象具體的容器、LiftCycle、容器的啟動(dòng)及停止、類型變量及實(shí)現(xiàn)類注入和兩者映射等操作

至于Provide和Populate的源碼相對(duì)比較簡(jiǎn)單易懂在這里不在描述

具體源碼

3、Extract(位于extract.go文件)

主要用于在application啟動(dòng)初始化過(guò)程通過(guò)依賴注入的方式將容器中的變量值來(lái)填充給定的struct,其中target必須是指向struct的指針,并且只能填充可導(dǎo)出的字段(golang只能通過(guò)反射修改可導(dǎo)出并且可尋址的字段),Extract將被Populate代替。 具體源碼

4、其他

諸如Populate是用來(lái)替換Extract的,而LiftCycle和inout.go涉及內(nèi)容比較多后續(xù)會(huì)單獨(dú)提供專屬文件說(shuō)明。

在Fx中提供的構(gòu)造函數(shù)都是惰性調(diào)用,可以通過(guò)invocations在application啟動(dòng)來(lái)完成一些必要的初始化工作:fx.Invoke(function); 通過(guò)也可以按需自定義實(shí)現(xiàn)LiftCycle的Hook對(duì)應(yīng)的OnStart和OnStop用來(lái)完成手動(dòng)啟動(dòng)容器和關(guān)閉,來(lái)滿足一些自己實(shí)際的業(yè)務(wù)需求。

Fx框架源碼解析

主要包括app.go、lifecycle.go、annotated.go、populate.go、inout.go、shutdown.go、extract.go(可以忽略,了解populate.go)以及輔助的internal中的fxlog、fxreflect、lifecycle

Go語(yǔ)言與Java之間性能相差多少

Java是一門較為成熟的語(yǔ)言,相對(duì)于C++要簡(jiǎn)單的多,C++里沒有內(nèi)存回收,所以比較麻煩,Java加入了內(nèi)存自動(dòng)回收,簡(jiǎn)單是簡(jiǎn)單,卻變慢了,go語(yǔ)言是一門新興的語(yǔ)言,現(xiàn)在版本是1.9 ? go語(yǔ)言的性能比Java要好,但由于出現(xiàn)晚,資料較Java少,有些Java的功能go也沒有,并且有許多的軟件是支持Java但支持go的很少.所以在短期內(nèi)Java是比go通用的

C語(yǔ)言的最大的優(yōu)勢(shì)是時(shí)間性能好,只比匯編慢20%~30%,C++最大的優(yōu)勢(shì)是快且面向?qū)ο?Java最大的優(yōu)勢(shì)是垃圾回收機(jī)制,GO語(yǔ)言的目標(biāo)是具備以上三者的優(yōu)勢(shì)

go依賴注入dig包使用-來(lái)自u(píng)ber公司

原文鏈接:

github:

Dependency Injection is the idea that your components (usually structs in go) should receive their dependencies when being created. This runs counter to the associated anti-pattern of components building their own dependencies during initialization. Let’s look at an example.

Suppose you have a Server struct that requires a Config struct to implement its behavior. One way to do this would be for the Server to build its own Config during initialization.

This seems convenient. Our caller doesn’t have to be aware that our Server even needs access to Config . This is all hidden from the user of our function.

However, there are some disadvantages. First of all, if we want to change the way our Config is built, we’ll have to change all the places that call the building code. Suppose, for example, our buildMyConfigSomehow function now needs an argument. Every call site would need access to that argument and would need to pass it into the building function.

Also, it gets really tricky to mock the behavior of our Config . We’ll somehow have to reach inside of our New function to monkey with the creation of Config .

Here’s the DI way to do it:

Now the creation of our Server is decoupled from the creation of the Config . We can use whatever logic we want to create the Config and then pass the resulting data to our New function.

Furthermore, if Config is an interface, this gives us an easy route to mocking. We can pass anything we want into New as long as it implements our interface. This makes testing our Server with mock implementations of Config simple.

The main downside is that it’s a pain to have to manually create the Config before we can create the Server . We’ve created a dependency graph here – we must create our Config first because of Server depends on it. In real applications these dependency graphs can become very large and this leads to complicated logic for building all of the components your application needs to do its job.

This is where DI frameworks can help. A DI framework generally provides two pieces of functionality:

A DI framework generally builds a graph based on the “providers” you tell it about and determines how to build your objects. This is very hard to understand in the abstract, so let’s walk through a moderately-sized example.

We’re going to be reviewing the code for an HTTP server that delivers a JSON response when a client makes a GET request to /people . We’ll review the code piece by piece. For simplicity sake, it all lives in the same package ( main ). Please don’t do this in real Go applications. Full code for this example can be found here .

First, let’s look at our Person struct. It has no behavior save for some JSON tags.

A Person has an Id , Name and Age . That’s it.

Next let’s look at our Config . Similar to Person , it has no dependencies. Unlike Person , we will provide a constructor.

Enabled tells us if our application should return real data. DatabasePath tells us where our database lives (we’re using sqlite). Port tells us the port on which we’ll be running our server.

Here’s the function we’ll use to open our database connection. It relies on our Config and returns a *sql.DB .

Next we’ll look at our PersonRepository . This struct will be responsible for fetching people from our database and deserializing those database results into proper Person structs.

PersonRepository requires a database connection to be built. It exposes a single function called FindAll that uses our database connection to return a list of Person structs representing the data in our database.

To provide a layer between our HTTP server and the PersonRepository , we’ll create a PersonService .

Our PersonService relies on both the Config and the PersonRepository . It exposes a function called FindAll that conditionally calls the PersonRepository if the application is enabled.

Finally, we’ve got our Server . This is responsible for running an HTTP server and delegating the appropriate requests to our PersonService .

The Server is dependent on the PersonService and the Config .

Ok, we know all the components of our system. Now how the hell do we actually initialize them and start our system?

First, let’s write our main() function the old fashioned way.

First, we create our Config . Then, using the Config , we create our database connection. From there we can create our PersonRepository which allows us to create our PersonService . Finally, we can use this to create our Server and run it.

Phew, that was complicated. Worse, as our application becomes more complicated, our main will continue to grow in complexity. Every time we add a new dependency to any of our components, we’ll have to reflect that dependency with ordering and logic in the main function to build that component.

As you might have guessed, a Dependency Injection framework can help us solve this problem. Let’s examine how.

The term “container” is often used in DI frameworks to describe the thing into which you add “providers” and out of which you ask for fully-build objects. The dig library gives us the Provide function for adding providers and the Invoke function for retrieving fully-built objects out of the container.

First, we build a new container.

Now we can add new providers. To do so, we call the Provide function on the container. It takes a single argument: a function. This function can have any number of arguments (representing the dependencies of the component to be created) and one or two return values (representing the component that the function provides and optionally an error).

The above code says “I provide a Config type to the container. In order to build it, I don’t need anything else.” Now that we’ve shown the container how to build a Config type, we can use this to build other types.

This code says “I provide a *sql.DB type to the container. In order to build it, I need a Config . I may also optionally return an error.”

In both of these cases, we’re being more verbose than necessary. Because we already have NewConfig and ConnectDatabase functions defined, we can use them directly as providers for the container.

Now, we can ask the container to give us a fully-built component for any of the types we’ve provided. We do so using the Invoke function. The Invoke function takes a single argument – a function with any number of arguments. The arguments to the function are the types we’d like the container to build for us.

The container does some really smart stuff. Here’s what happens:

That’s a lot of work the container is doing for us. In fact, it’s doing even more. The container is smart enough to build one, and only one, instance of each type provided. That means we’ll never accidentally create a second database connection if we’re using it in multiple places (say multiple repositories).

Now that we know how the dig container works, let’s use it to build a better main.

The only thing we haven’t seen before here is the error return value from Invoke . If any provider used by Invoke returns an error, our call to Invoke will halt and that error will be returned.

Even though this example is small, it should be easy to see some of the benefits of this approach over our “standard” main. These benefits become even more obvious as our application grows larger.

One of the most important benefits is the decoupling of the creation of our components from the creation of their dependencies. Say, for example, that our PersonRepository now needs access to the Config . All we have to do is change our NewPersonRepository constructor to include the Config as an argument. Nothing else in our code changes.

Other large benefits are lack of global state, lack of calls to init (dependencies are created lazily when needed and only created once, obviating the need for error-prone init setup) and ease of testing for individual components. Imagine creating your container in your tests and asking for a fully-build object to test. Or, create an object with mock implementations of all dependencies. All of these are much easier with the DI approach.

I believe Dependency Injection helps build more robust and testable applications. This is especially true as these applications grow in size. Go is well suited to building large applications and has a great DI tool in dig . I believe the Go community should embrace DI and use it in far more applications.

分享名稱:go語(yǔ)言需要依賴注入嗎,golang 依賴注入
標(biāo)題網(wǎng)址:http://chinadenli.net/article2/dsijcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站網(wǎng)站設(shè)計(jì)公司小程序開發(fā)微信公眾號(hào)手機(jī)網(wǎng)站建設(shè)全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)