Question
package main import ( encoding/json fmt strings os ) type Student struct { Name string RollNo int } type SecretStudent struct { SecretCode string ChunkSize
package main
import ( "encoding/json" "fmt" "strings" "os" )
type Student struct { Name string RollNo int }
type SecretStudent struct { SecretCode string ChunkSize int Length int StudentDetail Student }
func teacher(secretStudents []SecretStudent, c chan []byte) { for _, secretStudent := range secretStudents { // Modify the SecretCode chunks := chunkString(secretStudent.SecretCode, secretStudent.ChunkSize) secretStudent.SecretCode = reverseChunks(chunks)
// Convert struct to byte array b, _ := json.Marshal(secretStudent) c <- b } close(c) }
func management(c chan []byte) { for b := range c { // Decode byte array to struct var secretStudent SecretStudent json.Unmarshal(b, &secretStudent) fmt.Printf("Received secretStudent: %v ",secretStudent) // fmt.Println("Received From teacher:", secretStudent)
// Fix the SecretCode chunks := chunkString(secretStudent.SecretCode, secretStudent.ChunkSize) secretStudent.SecretCode = reverseChunks(chunks) fmt.Println("Fix the secretcode:", secretStudent) } os.Exit(0) }
func chunkString(s string, chunkSize int) []string { var chunks []string if len(s)%chunkSize != 0 { s += strings.Repeat("0", chunkSize-len(s)%chunkSize) } for i := 0; i < len(s); i += chunkSize { chunks = append(chunks, s[i:i+chunkSize]) } return chunks }
func reverseChunks(chunks []string) string { for i := 0; i < len(chunks)/2; i++ { chunks[i], chunks[len(chunks)-i-1] = chunks[len(chunks)-i-1], chunks[i] } return strings.Join(chunks, "") }
func main() { c := make(chan []byte) // Creating the SecretStudent objects secretStudents := []SecretStudent{ { SecretCode: "abcdefghijkl", ChunkSize: 4, Length: 12, StudentDetail: Student{ Name: "Vamsi", RollNo: 1, }, }, { SecretCode: "abcdefghijkl", ChunkSize: 5, Length: 12, StudentDetail: Student{ Name: "Praveen", RollNo: 2, }, }, { SecretCode: "abcdefghijkl", ChunkSize: 3, Length: 12, StudentDetail: Student{ Name: "Sunil", RollNo: 3, }, }, } go teacher(secretStudents, c) go management(c)
// wait for the goroutines var input string fmt.Scanln(&input) } ================================ Requirements:-
Output is not as expected there are trailing os in the end of the string,need to displayed the exact output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started