Explore key concepts in Go networking with this quiz covering TCP and UDP communications, socket handling, connection methods, and essential error handling techniques. Designed for Go beginners, this quiz helps reinforce knowledge of core principles needed for basic network programming.
Which function is used in Go to create a TCP client connection to a remote server?
Explanation: The net.Dial function in Go is used for creating a client-side connection to a remote server using protocols like TCP or UDP. net.Listen is used for starting a server that listens for incoming connections. net.Bind is not a valid Go function, and net.Accept is used to accept incoming connections on the server side, not to create them.
Which protocol should you use in Go for reliable, connection-oriented communication between two endpoints?
Explanation: TCP is a connection-oriented protocol that ensures reliable and ordered delivery of data between two endpoints. UDP is connectionless and does not guarantee delivery or order. ICMP is used for network diagnostics, not regular data transfer. HTTP is an application-level protocol that typically runs over TCP.
In Go, which function do you use to create a UDP socket for sending or receiving messages?
Explanation: net.ListenUDP is used in Go to create a UDP socket for sending and receiving datagrams. net.ConnectTCP and net.BindTCP are not valid Go functions. net.AcceptUDP does not exist, as UDP is connectionless and does not accept connections like TCP.
After establishing a TCP connection in Go, which method is commonly used to read data from the connection?
Explanation: The Read method is used to receive data from a TCP connection in Go. Send, Transmit, and Fetch are not standard Go methods for reading data from network connections. Using Read allows you to access incoming bytes from the connection efficiently.
What is the recommended way to handle network errors in Go after attempting to connect to a remote server?
Explanation: In Go, it is best practice to always check the error value returned by networking functions to handle problems such as connection failures. Automatic retries are not performed by default, ignoring errors can lead to unexpected behavior, and printing a warning without handling does not resolve the underlying issue.
Which method should you call in Go to properly close a network connection after you're done with it?
Explanation: The Close method is used to properly close open network connections in Go, signaling the end of communication and freeing resources. Disconnect, End, and Terminate are not valid Go connection methods; failing to use Close may leave the connection open and cause resource leaks.
What function is used in Go to start a server that listens for incoming TCP connections on a specific port?
Explanation: net.Listen is the function used to start listening for incoming TCP connections on a specified port. net.DialTCP is used for making outbound TCP connections. net.AcceptListen and net.Setup are not valid Go networking functions.
Which method allows you to send a UDP packet to a specified address in Go using a UDP connection object?
Explanation: WriteTo is used in Go to send a UDP datagram to a particular address using a UDP connection object. SendTo is not a standard Go method, PushData and Transfer are not valid UDP methods in Go, so they would not work for sending UDP packets.
When using net.Listen in Go to create a TCP server, what type of object do you typically receive to accept new connections?
Explanation: net.Listen returns a Listener object in Go, which is used to accept new incoming connections. Connector, Accepter, and Socket are incorrect; there is no standard type by those names provided for TCP listening in Go’s net package.
What is the valid range of port numbers that can be used when setting up a TCP or UDP server in Go?
Explanation: The valid port number range is from 0 to 65535. Ports 1 to 1024 are known as well-known ports, but higher ports are also commonly used. 1 to 32767 and 1000 to 65535 only cover partial ranges, so the complete correct range is 0 to 65535.