当前位置:网站首页>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]
边栏推荐
- 10 major network security incidents in the past 10 years
- Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
- golang启动报错【已解决】
- We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
- Making high-precision map based on autoware (V)
- How to protect WordPress website from network attack? It is essential to take safety measures
- Autoware reports an error: can't generate global path for start solution
- [netding cup 2020 rosefinch group]nmap
- TypeError: can only concatenate str (not “int“) to str
- Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation
猜你喜欢

Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
![[网鼎杯 2020 朱雀组]Nmap](/img/22/1fdf716a216ae26b9110b2e5f211f6.png)
[网鼎杯 2020 朱雀组]Nmap

Merkel Studio - harmonyos implementation list to do

【HCIP】两个MGRE网络通过OSPF实现互联(eNSP)

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

5G 商用第三年:无人驾驶的“上山”与“下海”

Planning mathematics final simulation exam I

明日无限计划,2022某公司元宇宙产品发布会活动概念策划方案

Lombook User Guide

【golang】使用select {}
随机推荐
Understand various paths
mysql的执行顺序
九天后我们一起,聚焦音视频、探秘技术新发展
Minimalist thrift+consumer
[hcip] experiment of republishing and routing strategy
[WesternCTF2018]shrine
Comprehensive upgrade, complete collection of Taobao / tmall API interfaces
Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation
Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
How to deal with the DDoS attack on the game server and how to defend it?
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
Where will Jinan win in hosting the first computing power conference?
动态内存与智能指针
[hcip] MPLS Foundation
T-sne降维
5G 商用第三年:无人驾驶的“上山”与“下海”
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
Ruiji takeout project actual battle day01
In depth analysis of C language memory alignment
DSP震动座椅