This is a simple Go API server that supports both GET and POST requests. The server allows the user to specify the port number via a command-line argument.
- GET /: Returns a welcome message.
- POST /post: Accepts JSON data and returns the same data in the response.
- Go 1.16 or later.
git clone https://github.com/sojoudian/go-api-server.git
cd go-api-server
You can specify the port number when starting the server. If no port number is provided, it defaults to 8091
.
go run main.go -port=8080
This will start the server on localhost:8080
.
If you want to use the default port (8091), simply run:
go run main.go
You can test the GET request by opening the following URL in your browser or using curl
:
curl http://localhost:8080/
You should see the following response:
Welcome to the Go API
To test the POST endpoint, you can use curl
to send a JSON payload:
curl -X POST -d '{"name": "John", "content": "Hello World!"}' http://localhost:8080/post -H "Content-Type: application/json"
The response should return the same JSON you sent:
{
"name": "John",
"content": "Hello World!"
}
To change the port, use the -port
flag when running the server. For example, to run on port 3000:
go run main.go -port=3000
.
├── main.go # Main Go file containing server logic
└── README.md # Project documentation
This project is licensed under the MIT License - see the LICENSE file for details.