Home  >  Article  >  Backend Development  >  golang does not build src

golang does not build src

WBOY
WBOYOriginal
2023-05-09 20:08:06532browse

Abstract:

This article introduces how to use golang without creating a src directory. By modifying the GOPATH environment variable, we can place the code files directly under the GOPATH path, avoiding the cumbersome process of creating src, pkg, bin and other directories, making development simpler and more efficient.

Text:

As a programming language with high performance and strong concurrency, Golang is increasingly used in the industry. Especially in the field of Internet development, its simplicity, efficiency and ease of use have won the favor of more and more developers. However, when using Golang for development, the code needs to be organized according to a certain directory structure, which makes novices somewhat helpless. This article introduces a method without creating a src directory when developing using Golang, which facilitates code organization and management.

  1. Why not create a src directory

When traditionally using golang for development, we usually create a directory under $GOPATH/src for storage Project code. However, for small-scale projects, this can be cumbersome. On the one hand, since a separate directory needs to be created for each project, the number of these directories may gradually increase, which will reduce the efficiency of project management. On the other hand, for some simple code snippets or small scripts, creating a separate directory seems to be a waste of space and time. At this time, using the method of not creating a src directory can not only avoid the above problems, but also improve development efficiency, allowing us to focus more on code writing.

  1. How not to create the src directory

When using Golang for development, we can modify the value of the environment variable GOPATH to achieve the method of not creating the src directory. The specific operations are as follows:

2.1 Create a directory

First we need to create a directory as our working directory. This directory can store code files for multiple projects. There is no need to create any additional directories. The working directory can be named whatever you want, such as ~/go.

$ mkdir ~/go
$ cd ~/go

2.2 Set the GOPATH environment variable

Modify the value of the environment variable GOPATH to point to our working directory~/ go so that the Go compiler will look for code files in our working directory.

$ export GOPATH=~/go

Note: The value of this environment variable must be a path list, which is composed of multiple paths. At this time, we only define one path, that is ~/go, if you want to find code files in multiple paths, you need to separate them with colons ":".

2.3 Writing code

Now we can create our code file in the ~/go directory and write our Golang code.

2.4 Build and Run

We can build and run our Golang code as usual. For example, here is a Hello World example.

$ cd ~/go
$ echo 'package main; import "fmt"; func main() { fmt.Println("Hello, world!") }' > hi.go
$ go run hi.go

  1. Summary

When using Golang for development, the method of not creating a src directory is suitable for small-scale projects and simple code snippets Very practical. By modifying the GOPATH environment variable, we can directly place all code files under GOPATH, avoiding the cumbersome process of creating src, pkg, bin and other directories, making development simpler and more efficient.

The above is the detailed content of golang does not build src. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn