当前位置:网站首页>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]
边栏推荐
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
- ELS square movement
- ELS stop at all
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- [网鼎杯 2020 朱雀组]Nmap
- 抓包工具Charles使用
- 明日无限计划,2022某公司元宇宙产品发布会活动概念策划方案
- With the explosive growth of digital identity in 2022, global organizations are facing greater network security
- [hcip] MPLS Foundation
- 10 major network security incidents in the past 10 years
猜你喜欢

Cross modal alignment 20220728

TDA75610-I2C-模拟功放I2C地址的确定

560 和为 K 的子数组

易观分析:以用户为中心,提升手机银行用户体验,助力用户价值增长

【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”

Sigma-DSP-OUTPUT

规划数学期末模拟考试一
![[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]](/img/0b/052d66712b987e3507aa6dfadcabe4.png)
[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]

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

Six noteworthy cloud security trends in 2022
随机推荐
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
九天后我们一起,聚焦音视频、探秘技术新发展
[WesternCTF2018]shrine
After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
把逻辑做在Sigma-DSP中的优化实例-数据分配器
Timer of BOM series
5g commercial third year: driverless "going up the mountain" and "going to the sea"
BOM系列之定时器
Internship: tool class writing for type judgment
[unity project practice] synthetic watermelon
Slow storage scheme
J9 number theory: what factors determine the value of NFT?
Read the recent trends of okaleido tiger and tap the value and potential behind it
Making high-precision map based on autoware (V)
Cross modal alignment 20220728
Network security litigation risk: four issues that chief information security officers are most concerned about
Embedded sharing collection 23