package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Email string `json:"email"`
}
func main() {
person := Person{
Name: "LoisMay",
Age: 21,
Email: "[email protected]",
}
// 序列化为 JSON 字节数组
jsonData, err := json.Marshal(person)
if err != nil {
fmt.Println("JSON 序列化错误:", err)
return
}
// 打印 JSON 字符串
fmt.Println(string(jsonData))
// {"name":"LoisMay","age":21,"email":"[email protected]"}
}
func main() {
Data := []byte(`{"name":"LoisMay","age":21,"email":"[email protected]"}`)
// 反序列化为 Person 结构体
var person Person
err := json.Unmarshal(Data, &person)
if err != nil {
fmt.Println("JSON 反序列化错误:", err)
return
}
// 打印反序列化后的对象
fmt.Println(person.Name)
fmt.Println(person.Age)
fmt.Println(person.Email)
}
goto 语句可以用来实现程序的无条件跳转
但在实际开发中,应该谨慎使用它,因为滥用 goto 可能会导致代码结构混乱和可读性降低。
package main
import "fmt"
func main() {
var n = 30
fmt.Println("LoisMay")
if n > 20 {
goto func1
}
fmt.Println("LoisMays")
fmt.Println("LoisMayss")
fmt.Println("LoisMaysss")
func1:
fmt.Println("666")
fmt.Println("777")
fmt.Println("888")
}
// LoisMay
// 666
// 777
// 888
Server.go
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
func process(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
for {
fmt.Printf("Server is waiting for client message from %s:\n", conn.RemoteAddr().String())
massage, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("Client exited with error: %v\n", err)
return
}
massage = strings.Trim(massage, "\r\n")
fmt.Printf("Received message from client: %s\n", massage)
// 服务端回复消息
response := "Server received: " + massage + "\n"
_, err = conn.Write([]byte(response))
if err != nil {
fmt.Printf("Error sending response to client: %v\n", err)
return
}
}
}
func main() {
fmt.Println("Server is Listen")
listen, err := net.Listen("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println("Listen err=", err)
return
}
defer listen.Close()
for {
fmt.Println("Waiting for client connection")
conn, err := listen.Accept()
if err != nil {
fmt.Println("Accept err=", err)
} else {
fmt.Printf("Accepted connection from client: %v\n", conn.RemoteAddr().String())
}
go process(conn)
}
}
Client.go
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func main() {
conn, err := net.Dial("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println("Client dial error:", err)
return
}
defer conn.Close()
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter message: ")
message, err := reader.ReadString('\n')
if err != nil {
fmt.Println("ReadString error:", err)
break
}
message = strings.Trim(message, "\r\n")
if message == "exit" {
fmt.Println("Client is exiting")
break
}
_, err = conn.Write([]byte(message + "\n"))
if err != nil {
fmt.Println("Connection write error:", err)
break
}
response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Println("Read response error:", err)
break
}
fmt.Println("Server response:", response)
}
}
server.go
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func handleClient(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
for {
// 读取客户端消息
message, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("Error reading client message: %v\n", err)
return
}
message = strings.Trim(message, "\r\n")
fmt.Printf("Received message from client: %s\n", message)
// 回复客户端消息
response := "Server received: " + message + "\n"
_, err = writer.WriteString(response)
if err != nil {
fmt.Printf("Error sending response to client: %v\n", err)
return
}
writer.Flush()
// 检查客户端是否要退出
if message == "exit" {
fmt.Println("Client is exiting")
return
}
}
}
func main() {
fmt.Println("Server is listening")
listener, err := net.Listen("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println("Listen error:", err)
return
}
defer listener.Close()
for {
fmt.Println("Waiting for client connection")
conn, err := listener.Accept()
if err != nil {
fmt.Println("Accept error:", err)
break
}
fmt.Println("Client connected:", conn.RemoteAddr().String())
go handleClient(conn)
// 启动一个 goroutine 处理服务端发送消息
go func(c net.Conn) {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(c)
for {
// 从控制台读取输入
fmt.Print("Enter message: ")
input, _ := reader.ReadString('\n')
input = strings.Trim(input, "\r\n")
// 发送消息给客户端
_, err := writer.WriteString(input + "\n")
if err != nil {
fmt.Printf("Error sending message to client: %v\n", err)
return
}
writer.Flush()
// 检查服务端是否要退出
if input == "exit" {
fmt.Println("Server is exiting")
return
}
}
}(conn)
}
// 阻塞主线程,使服务端持续运行
<-make(chan struct{})
}
Client.go
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func handleServer(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
for {
// 读取服务端消息
message, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("Error reading server message: %v\n", err)
return
}
message = strings.Trim(message, "\r\n")
fmt.Printf("Received message from server: %s\n", message)
// 检查服务端是否要退出
if message == "exit" {
fmt.Println("Server is exiting")
return
}
// 从控制台读取输入
fmt.Print("Enter message: ")
input, _ := reader.ReadString('\n')
input = strings.Trim(input, "\r\n")
// 发送消息给服务端
_, err = writer.WriteString(input + "\n")
if err != nil {
fmt.Printf("Error sending message to server: %v\n", err)
return
}
writer.Flush()
// 检查客户端是否要退出
if input == "exit" {
fmt.Println("Client is exiting")
return
}
}
}
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8888")
if err != nil {
fmt.Println("Client dial error:", err)
return
}
fmt.Println("Connected to server")
go handleServer(conn)
// 从控制台读取输入
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter message: ")
input, _ := reader.ReadString('\n')
input = strings.Trim(input, "\r\n")
// 发送消息给服务端
_, err = conn.Write([]byte(input + "\n"))
if err != nil {
fmt.Printf("Error sending message to server: %v\n", err)
return
}
// 检查客户端是否要退出
if input == "exit" {
fmt.Println("Client is exiting")
break
}
}
conn.Close()
}