One of the most powerful aspects of working in a monorepo is the ability to share code between packages/teams/hierarchies. In this post I will try to explain a very simple real world scenario
Example Scenario
Imagine you want to develop a library to show the file sizes in megabytes which you feel might be useful to other parts of your monorepo. The library accepts the size as Integer (ex: 2048 bytes) and can return a humanized string (ex: 2 MB). To add some quality assurance, we will also write a test for the same.
How does Bazel enable sharing code?
From the above scenario we are aware that we need to develop this function as a shared library which then be imported by another package for usage. Bazel makes this extremely simple by allowing us to define the function in a library and export it other services that will need it. As explained in my earlier post linked at the bottom of this post, we can also control which other libraries can be allowed to import it for usage as well.
Let's get coding
For code organization purpose, we will have a libraries directory at the root of our workspace with a child directory called humanize_filesize which is where we will write our library code.
Let's write some very elementary Go code in humanize_filesize.go
package humanize_filesize import "fmt" // GetHumanizedFilesize takes size_in_bytes as an int32 pointer and returns the size in megabytes. func GetHumanizedFilesize(size_in_bytes *int32) string { if size_in_bytes != nil { size_in_megabytes := float64(*size_in_bytes) / (1024 * 1024) return fmt.Sprintf("%.4f MB", size_in_megabytes) } return "0 MB" }
This code simply takes an int32 as an input and returns a computed readable megabyte string to 4 decimal precision
This function is definitely not comprehensive and can definitely be improved, but that's not the point of this exercise.
Also assert that our logic is working as intended, we will add a very elementary test alongside our go code in a file called humanize_filesize_test.go
package humanize_filesize import ( "testing" ) func TestHumanizeFilesize(t *testing.T) { tests := []struct { name string size_in_bytes *int32 expected string }{ { name: "nil bytes", size_in_bytes: nil, expected: "0 MB", }, { name: "2048 bytes", size_in_bytes: int32Ptr(2048), expected: "0.0020 MB", }, { name: "0 bytes", size_in_bytes: int32Ptr(0), expected: "0.0000 MB", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetHumanizedFilesize(tt.size_in_bytes) if result != tt.expected { t.Errorf("expected %s, got %s", tt.expected, result) } }) } } func int32Ptr(n int32) *int32 { return &n }
A very simple test with basic tests for nil, int32 and 0 as inputs
Now comes the juicy part of how to export this function so that this can be imported within other packages or services. This is where we have to define the BUILD.bazel file.
load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "humanize_filesize", srcs = ["humanize_filesize.go"], importpath = "basil/libraries/humanize_filesize", visibility = ["//visibility:public"], ) go_test( name = "humanize_filesize_test", srcs = ["humanize_filesize_test.go"], embed = [":humanize_filesize"], )
In here we are defining two main rules. One for the actual library and one for the test file that we wrote.
The go_library defines that the target humanize_filesize uses humanize_filesize.go as one of its sources which can be imported by the path specified in importpath and it is visible publicly within the workspace for other packages to import. We will learn how to control the visibility in a future post.
The go_test defines a test target which embeds the code from the output of go_library.
At this point we should be able to test the library by running our test suite as following
bazel build //... && bazel run //libraries/humanize_filesize:humanize_filesize_test
You should be able to see the test output as following indicating that all the tests have passed.
package humanize_filesize import "fmt" // GetHumanizedFilesize takes size_in_bytes as an int32 pointer and returns the size in megabytes. func GetHumanizedFilesize(size_in_bytes *int32) string { if size_in_bytes != nil { size_in_megabytes := float64(*size_in_bytes) / (1024 * 1024) return fmt.Sprintf("%.4f MB", size_in_megabytes) } return "0 MB" }
? Wohoo!!! ? Now we know that our library is working as intended.
Now let's use this library in a service service1 within a services directory that we will create at the root of the workspace with the following go code and BUILD.bazel file.
service1.go
package humanize_filesize import ( "testing" ) func TestHumanizeFilesize(t *testing.T) { tests := []struct { name string size_in_bytes *int32 expected string }{ { name: "nil bytes", size_in_bytes: nil, expected: "0 MB", }, { name: "2048 bytes", size_in_bytes: int32Ptr(2048), expected: "0.0020 MB", }, { name: "0 bytes", size_in_bytes: int32Ptr(0), expected: "0.0000 MB", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetHumanizedFilesize(tt.size_in_bytes) if result != tt.expected { t.Errorf("expected %s, got %s", tt.expected, result) } }) } } func int32Ptr(n int32) *int32 { return &n }
BUILD.bazel
load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "humanize_filesize", srcs = ["humanize_filesize.go"], importpath = "basil/libraries/humanize_filesize", visibility = ["//visibility:public"], ) go_test( name = "humanize_filesize_test", srcs = ["humanize_filesize_test.go"], embed = [":humanize_filesize"], )
The go code is pretty simple which imports our library that we declared earlier and uses the GetHumanizedFilesize function from our library and passes a random integer value and prints the output.
Now when execute bazel build //services/service1 , bazel will resolve all dependencies for our target including the library that we developed and build them.
service1 can now be executed using bazel run //services/service1 since we have only one binary target defined. If you have more than one binary targets, ex: serviceX, you can execute that using bazel run //services/service1:serviceX. By default when not specifying a target, bazel will always try to find a binary target with the same name as the directory and run that.
So... there you go. We have made your first shared library that can be used by other parts of our monorepo.
All code for this example can be found at https://github.com/nixclix/basil/pull/3/commits/61c673b8757860bd5e60eb2ab6c35f3f4da78c87
If you like the content of this post feel free to share it. Also, please subscribe and leave comments on what you think about this post and if there are things that you would like to see me improving on.
The above is the detailed content of A practical example of shared libraries in a monorepo. For more information, please follow other related articles on the PHP Chinese website!

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment