Is learning source code, through Goland editor, simple and convenient generation of UML class diagram, can better understand the project. There seems to be no easy way to find a large circle on the Internet. Finally, I completed this goal by referring to GoLand and using PlantUML to generate Go UML diagrams. Thank you!! But this article for my environment, also experienced a night many hours to toss success. Now write down the process and hopefully help other students use it directly and quickly.

Environment introduction

For Windows 10, Goland is JetBrains Goland 2018.3.1×64. Before starting, ensure that the following software has been installed and added to the PATH. GOROOT and GOPATH, who are not yet clear, suggest looking at other documents to make sense of the two noun concepts.

  • Java
  • Golang
  • GoLand
  • GraphvizClick on the link on the left, select the MSI download from the image below, and double-click directly to complete the installation.

Install PlantUML plugin in GoLand

  • Go to File -> Settings -> Plugins -> Marketplace
  • Search box inputPlantUMLAnd click Install. The installation will take effect after restart

Install the Go-package-Plantuml tool

This step took a long time. Go get git.oschina.net/jscode/go-package-plantuml execute the commands are failure. Through the Internet to find reasons and keep trying to sum up, the steps are as follows:

  • I created a directory under drive D asplantUml, while putting this pathD:\plantUmlAdd to the system’s GOPATH. Clone the project and place it in the plantUml directory through git commandgit clone https://gitee.com/jscode/go-package-plantuml.git.
  • Add directory src\git.oschina.net\jscode under plantUml and copy the whole project directory. (Don’t ask why, it’s been tried here for a long time and I haven’t figured it out yet). The final path looks like this:
  • Into thego-package-plantumlDirectory, that is, a layer with the main.go file, executedgo build.
$ go build
go: GOPATH entry is relative; must be absolute path: "".
For more details see: 'go help gopath'
Copy the code

The above error can be made by deleting the last symbol ‘in the GOPATH path; ‘can be resolved to generate go-package-plantuml.exe executable file.

Add External Tools to GoLand

  • Go to File -> Settings -> Tools -> External Tools
  • Name: Enter the Name of a plantUml
  • Program: Select the path of the go-package-plantuml.exe execution file generated above
  • The Arguments: set to--gopath $GOPATH$ --codedir $FileDir$ --outputfile $FileDir$.puml

GoLand generates UML diagrams

  • Right-click the source directory and select External Tools to execute
  • Generate *. Puml file, double-click directly open to view. If your GoLand is configured with multiple addresses, it will fail. Change it to only the current project. At this point, HOWEVER, I find that the UML class diagram is not actually displayed, but rather an error is reported. So I began to debug and modify the source code step by step.

The source code to modify

  • The main.go file changes the following two points. Use the filepath library instead of the path library, otherwise the filepath will not be found because of the path file under the window.
VendorDir : path.Join(opts.CodeDir, "vendor"-- "VendorDir: filepath.Join(opts.codedir,"vendor")
result.OutputToFile("/tmp/uml.txt") - the result. OutputToFile (opts. OutputFile)Copy the code
  • Codeanalysis. Go file. Also change the path library to the Filepath library.
func (this *interfaceMeta) UniqueNameUML() string {
	return packagePathToUML(this.PackagePath) + "."+ this.name} -- "func (this *interfaceMeta) UniqueNameUML() string {return this.Name
}

func (this *structMeta) UniqueNameUML() string {
	return packagePathToUML(this.PackagePath) + "."+ this.name} -- "func (this *structMeta) UniqueNameUML() string {return this.Name
}

if(strings. HasPrefix (filepath, this. Config. VendorDir)) -"if (strings.HasPrefix(this.config.VendorDir, filePath))
ifStrings. HasPrefix (filepath, srcdir) -"if strings.HasPrefix(srcdir, filePath)
Copy the code
  • Import the modified EXE, you can normally generate UML diagrams in Goland. For large packages, some areas need to be handled manually, but it’s generally acceptable. Will continue to optimize the source code to achieve better.

Change the generated EXE, you can download here.

Link: pan.baidu.com/s/1I5GWbu6R… Extraction code: 2T6R