Home  >  Article  >  Backend Development  >  Let's talk about the cyclic dependency detection tool (go-cyclic) in Go

Let's talk about the cyclic dependency detection tool (go-cyclic) in Go

藏色散人
藏色散人forward
2022-12-12 16:47:566473browse

This article will introduce you to the relevant knowledge about Golang and talk about a cyclic dependency detection tool (go-cyclic) in open source Go. I hope it will be helpful to you.

go-cyclic cyclic dependency resolution tool

Problem description

As a Go developer , we often encounter package circular dependency problems in large projects.
If there is a circular dependency in Golang, an exception will be thrown during compilation.

When do circular dependencies occur?

If there are two packages: package a and package b. When package a depends on package b, and package b depends on package a, a circular dependency will occur. The real situation may be more complicated. For example, if package a depends on package b, package b depends on package c, and package c depends on package a, a cycle is formed.
Lets talk about the cyclic dependency detection tool (go-cyclic) in GoLets talk about the cyclic dependency detection tool (go-cyclic) in Go

import cycle not allowed

And if the project is very large, there are many .go files under a package, and only circular references between packages are prompted (as shown above), which is difficult to locate. Which .go file and which .go file are directly referenced in a circular manner, which is difficult to check.

go-cyclic simple usage

Github address: github.com/elza2/go-cyclic (Welcome to submit PRs)

go-cyclic can be used to check whether there are cyclic dependencies in the project and locate specific .go files. Helps quickly locate loop problems.

Quick Start

go install github.com/elza2/go-cyclic@latest
# path 路径要设置为 go.mod 文件所在的路径.go-cyclic gocyclic --dir .path
rrree

Lets talk about the cyclic dependency detection tool (go-cyclic) in Go

The above is the detailed content of Let's talk about the cyclic dependency detection tool (go-cyclic) in Go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete