code: func WriteState(d *State, dst io.Writer) error {
  	// writing a nil state is a noop.
  	if d == nil {
  		return nil
  	}
  
  	// make sure we have no uninitialized fields
  	d.init()
  
  	// Make sure it is sorted
  	d.sort()
  
  	// Ensure the version is set
  	d.Version = StateVersion
  
  	// If the TFVersion is set, verify it. We used to just set the version
  	// here, but this isn't safe since it changes the MD5 sum on some remote
  	// state storage backends such as Atlas. We now leave it be if needed.
  	if d.TFVersion != "" {
  		if _, err := version.NewVersion(d.TFVersion); err != nil {
  			return fmt.Errorf(
  				"Error writing state, invalid version: %s\n\n"+
  					"The Terraform version when writing the state must be a semantic\n"+
  					"version.",
  				d.TFVersion)
  		}
  	}
  
  	// Encode the data in a human-friendly way
  	data, err := json.MarshalIndent(d, "", "    ")
  	if err != nil {
  		return fmt.Errorf("Failed to encode state: %s", err)
  	}
  
  	// We append a newline to the data because MarshalIndent doesn't
  	data = append(data, '\n')
  
  	// Write the data out to the dst
  	if _, err := io.Copy(dst, bytes.NewReader(data)); err != nil {
  		return fmt.Errorf("Failed to write state: %v", err)
  	}
  
  	return nil
  }
docs: // WriteState writes a state somewhere in a binary format.