当前位置:网站首页>Golang run times undefined error [resolved]
Golang run times undefined error [resolved]
2022-07-29 01:51:00 【JavaPub-rodert】
List of articles
problem
stay golang There are two files in the same package in , Respectively main.go and server.go,main Function in main.go in , And in main Function refers to server.go A structure in
Run time error :
PS E:\myworkpace\goworkpace\go_im> go run .\main.go
# command-line-arguments
.\main.go:9:12: undefined: NewServer
reason
go run: Compile first , The compilation here will only take this main.go Subject to , It leads to the situation that other files referenced cannot be found
go build: Dissimilarity , It will automatically find the reference file and package
solve
Method 1 :
go run main.go a.go
Method 2 :
go run *.go
Method 3 :
go build

Code case
main.go
package main
import "fmt"
func main() {
fmt.Println("starting")
server := NewServer("127.0.0.1", 8888)
server.Start()
}
server.go
package main
import (
"fmt"
"net"
)
type Server struct {
Ip string
Port int
}
// Create a server The interface of
func NewServer(ip string, port int) *Server {
server := &Server{
Ip: ip,
Port: port,
}
return server
}
func (s *Server) Handler(conn net.Conn) {
// Currently connected services
fmt.Println(" Connection established successfully !")
}
// Start the interface of the server
func (s *Server) Start() {
// socket listen
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Ip, s.Port))
if err != nil {
fmt.Println("net.Listen err: ", err)
return
}
// close listen socket
defer listener.Close()
for {
// accpet
conn, err := listener.Accept()
if err != nil {
fmt.Println("listener accept err: ", err)
continue
}
// do handler
go s.Handler(conn)
}
}
go.mod
module javapub.net/go_im
go 1.18
Project initialization method ,
go mod init [module]
边栏推荐
- Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
- 10 major network security incidents in the past 10 years
- Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
- matplotlib中文问题
- With the explosive growth of digital identity in 2022, global organizations are facing greater network security
- SiC Power Semiconductor Industry Summit Forum successfully held
- Sigma-DSP-OUTPUT
- [WesternCTF2018]shrine
- 易观分析:以用户为中心,提升手机银行用户体验,助力用户价值增长
- Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
猜你喜欢

【Web技术】1395- Esbuild Bundler HMR

【搜索】—— 迭代加深/双向DFS/IDA*

Window object of BOM series

Reinforcement learning (II): SARS, with code rewriting

Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security

10 major network security incidents in the past 10 years

使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包

Make logic an optimization example in sigma DSP - data distributor

SiC Power Semiconductor Industry Summit Forum successfully held
![About df['a column name'] [serial number]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
About df['a column name'] [serial number]
随机推荐
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
新生代公链再攻「不可能三角」
StoneDB 邀请您参与开源社区月会!
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
T-sne dimensionality reduction
[WesternCTF2018]shrine
TypeError: can only concatenate str (not “int“) to str
LeetCode 112:路径总和
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
numpy. Where() usage and np.argsort() usage
Some summaries of ibatis script and provider
Lombook User Guide
Minimalist thrift+consumer
What are the common cyber threats faced by manufacturers and how do they protect themselves
Six noteworthy cloud security trends in 2022
SiC功率半导体产业高峰论坛成功举办
采用QT进行OpenGL开发(二)绘制立方体
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
【golang】使用select {}
T-sne降维